summaryrefslogtreecommitdiffstats
path: root/binding-bluetooth/bluetooth-agent.c
blob: 7193ce514be1dbb33a2ac819af3be76d4607f36c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*  Copyright 2016 ALPS ELECTRIC CO., LTD.
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <gio/gio.h>

#include "bluetooth-agent.h"

#ifdef AGENT_THREAD

#include <pthread.h>

static GMainLoop *agentLoop = NULL;
#endif

static GDBusConnection *system_conn = NULL;
static gboolean agent_registered = FALSE;
static const char *agent_capability = NULL;
static Agent_RegisterCallback_t agent_RegisterCallback = { 0 };
static AGENTOrgBluezAgent1 * agnet_interface = NULL;

static struct Agent_Node *agent_event = NULL;


static void agent_event_auto_release(struct Agent_Node *event)
{
    if (NULL == event ){
        return;
    }
    g_dbus_method_invocation_return_dbus_error (event->invocation,
                                                ERROR_BLUEZ_REJECT, "");
    g_free(event);
}

/*
 * Store the agent event
 */
static void agent_event_new(AGENTOrgBluezAgent1      *object,
                    GDBusMethodInvocation   *invocation, int type)
{
    LOGD("\n");
    if(NULL !=agent_event )
    {
        agent_event_auto_release(agent_event);
    }
    agent_event = g_malloc0(sizeof(struct Agent_Node));
    agent_event->type = type;
    agent_event->object = object;
    agent_event->invocation = invocation;

}


/*
 * agent api
 *
 * Methods : void Release()
 * This method gets called when the service daemon
 * unregisters the agent. An agent can use it to do
 * cleanup tasks. There is no need to unregister the
 * agent, because when this method gets called it has
 * already been unregistered.
 */
static gboolean
on_handle_Release (AGENTOrgBluezAgent1      *object,
                    GDBusMethodInvocation   *invocation,
                    gpointer                user_data)
{
    LOGD("\n");

    //gboolean ret = FALSE;

    if (NULL != agent_RegisterCallback.agent_Release)
    {
        agent_RegisterCallback.agent_Release();
    }

    agent_org_bluez_agent1_complete_release(object, invocation);

    g_dbus_interface_skeleton_unexport(
        G_DBUS_INTERFACE_SKELETON(agnet_interface));

    g_object_unref(agnet_interface);
    agent_registered = FALSE;

#ifdef AGENT_THREAD
    if(agentLoop){
        g_main_loop_quit(agentLoop);
    }
#endif

    return TRUE;
}

/*
 * agent api
 * Methods : string RequestPinCode(object device)
 *
 * This method gets called when the service daemon
 * needs to get the passkey for an authentication.
 *
 * The return value should be a string of 1-16 characters
 * length. The string can be alphanumeric.
 */
static gboolean
on_handle_RequestPinCode (AGENTOrgBluezAgent1       *object,
                            GDBusMethodInvocation   *invocation,
                            const gchar             *device,
                            gpointer                user_data)
{

    LOGD("device:%s.\n",device);

    gboolean ret = FALSE;
    const gchar *error = ERROR_BLUEZ_REJECT;
    gchar *response;

    if (NULL != agent_RegisterCallback.agent_RequestPinCode)
    {
        ret = agent_RegisterCallback.agent_RequestPinCode (device,
                                                           &response, &error);
    }

    if (TRUE == ret)
    {
        agent_org_bluez_agent1_complete_request_pin_code(object,
                                                        invocation, response);
        g_free(response);

    }
    else
    {
         g_dbus_method_invocation_return_dbus_error (invocation, error, "");
    }

    return TRUE;
}

/*
 * agent api
 * Methods : void DisplayPinCode(object device, string pincode)
 *
 * This method gets called when the service daemon
 * needs to display a pincode for an authentication.
 */
static gboolean
on_handle_DisplayPinCode (AGENTOrgBluezAgent1   *object,
                        GDBusMethodInvocation   *invocation,
                        const gchar             *device,
                        const gchar             *pincode,
                        gpointer                user_data)
{

    LOGD("device:%s,pincode:%s.\n",device, pincode);

    gboolean ret = FALSE;
    const gchar *error = ERROR_BLUEZ_REJECT;

    if (NULL != agent_RegisterCallback.agent_DisplayPinCode)
    {
        ret = agent_RegisterCallback.agent_DisplayPinCode(device,
                                                            pincode, &error);
    }

    if (TRUE == ret)
    {
        agent_org_bluez_agent1_complete_display_pin_code(object, invocation);
    }
    else
    {
        g_dbus_method_invocation_return_dbus_error (invocation, error, "");
    }

     return TRUE;
}


/*
 * agent api
 * Methods : uint32 RequestPasskey(object device)
 *
 * This method gets called when the service daemon
 * needs to get the passkey for an authentication.
 *
 * The return value should be a numeric value
 * between 0-999999.
 */
static gboolean
on_handle_RequestPasskey (AGENTOrgBluezAgent1   *object,
                        GDBusMethodInvocation   *invocation,
                        const gchar             *device,
                        gpointer                user_data)
{

    LOGD("device:%s.\n",device);

    gboolean ret = FALSE;
    const gchar *error = ERROR_BLUEZ_REJECT;
    guint passkey;

    if (NULL != agent_RegisterCallback.agent_RequestPasskey)
    {
        ret = agent_RegisterCallback.agent_RequestPasskey(device,
                                                            &passkey, &error);
    }

    if (TRUE == ret)
    {
        agent_org_bluez_agent1_complete_request_passkey(object,
                                                        invocation, passkey);
    }
    else
    {
        g_dbus_method_invocation_return_dbus_error (invocation, error, "");
    }

    return TRUE;

}


/*
 * agent api
 * Methods : void DisplayPasskey(object device, uint32 passkey, uint16 entered)
 *
 * This method gets called when the service daemon
 * needs to display a passkey for an authentication.
 *
 * The entered parameter indicates the number of already
 * typed keys on the remote side.
 */
static gboolean
on_handle_DisplayPasskey (AGENTOrgBluezAgent1   *object,
                        GDBusMethodInvocation   *invocation,
                        const gchar             *device,
                        guint                   passkey,
                        guint16                 entered,
                        gpointer                user_data)
{

    LOGD("device:%s,passkey:%d,entered:%d.\n",device, passkey, entered);

    //gboolean ret = FALSE;

    if (NULL != agent_RegisterCallback.agent_DisplayPasskey)
    {
        agent_RegisterCallback.agent_DisplayPasskey(device, passkey, entered);
    }

    agent_org_bluez_agent1_complete_display_passkey(object, invocation);


    return TRUE;

}

/*
 * agent api
 * Methods : void RequestConfirmation(object device, uint32 passkey)
 *
 * This method gets called when the service daemon
 * needs to confirm a passkey for an authentication.
 *
 * To confirm the value it should return an empty reply
 * or an error in case the passkey is invalid.
 */
static gboolean
on_handle_RequestConfirmation (AGENTOrgBluezAgent1  *object,
                            GDBusMethodInvocation   *invocation,
                            const gchar             *device,
                            guint                   passkey,
                            gpointer                user_data)
{
    LOGD("device:%s,passkey:%d.\n", device, passkey);

    gboolean ret = FALSE;
    const gchar *error = ERROR_BLUEZ_REJECT;

    if (NULL != agent_RegisterCallback.agent_RequestConfirmation)
    {
        ret = agent_RegisterCallback.agent_RequestConfirmation(device,
                                                              passkey, &error);
    }
    LOGD("ret %d\n",ret);
    if (TRUE == ret)
    {
        agent_event_new(object, invocation,REQUEST_CONFIRMATION);
        //agent_org_bluez_agent1_complete_request_confirmation(object,
        //                                                    invocation);
    }
    else
    {
        g_dbus_method_invocation_return_dbus_error (invocation, error, "");
    }

    return TRUE;
}

/*
 * agent api
 * Methods : void RequestAuthorization(object device)
 *
 * This method gets called to request the user to
 * authorize an incoming pairing attempt which
 * would in other circumstances trigger the just-works
 * model.
 */
static gboolean
on_handle_RequestAuthorization (AGENTOrgBluezAgent1     *object,
                                GDBusMethodInvocation   *invocation,
                                const gchar             *device,
                                gpointer                user_data)
{
    LOGD("device:%s.\n",device);

    gboolean ret = FALSE;
    const gchar *error = ERROR_BLUEZ_REJECT;

    if (NULL != agent_RegisterCallback.agent_RequestAuthorization)
    {
        ret = agent_RegisterCallback.agent_RequestAuthorization(device,&error);
    }

    if (TRUE == ret)
    {
        agent_org_bluez_agent1_complete_request_authorization(object,
                                                                invocation);
    }
    else
    {
        g_dbus_method_invocation_return_dbus_error (invocation, error, "");
    }

    return TRUE;
}

/*
 * agent api
 * Methods : void AuthorizeService(object device, string uuid)
 *
 * This method gets called when the service daemon
 * needs to authorize a connection/service request.
 */
static gboolean
on_handle_AuthorizeService (AGENTOrgBluezAgent1     *object,
                            GDBusMethodInvocation   *invocation,
                            const gchar             *device,
                            const gchar             *uuid,
                            gpointer                user_data)
{

    LOGD("device:%s.uuid:%s\n",device,uuid);

    gboolean ret = FALSE;
    const gchar *error = ERROR_BLUEZ_REJECT;

    if (NULL != agent_RegisterCallback.agent_AuthorizeService)
    {
        ret = agent_RegisterCallback.agent_AuthorizeService(device,
                                                            uuid, &error);
    }

    if (TRUE == ret)
    {
        agent_org_bluez_agent1_complete_authorize_service(object, invocation);
    }
    else
    {
        g_dbus_method_invocation_return_dbus_error (invocation, error, "");
    }

    return TRUE;
}

/*
 * agent api
 * Methods : void Cancel()
 *
 * This method gets called to indicate that the agent
 * request failed before a reply was returned.
 */
static gboolean
on_handle_Cancel (AGENTOrgBluezAgent1           *object,
                       GDBusMethodInvocation    *invocation,
                       gpointer                 user_data)
{
    LOGD("\n");

    if (NULL != agent_RegisterCallback.agent_Cancel)
    {
        agent_RegisterCallback.agent_Cancel();
    }

    agent_org_bluez_agent1_complete_cancel(object, invocation);

    return TRUE;

}

/*
 * agent init
 * init the dbus and register the agent to bluez
 */
static int create_and_register_agent(const char *capability)
{
    GError *error = NULL;
    gboolean ret;
    GVariant *value;

    LOGD("%s\n",capability);

    if (agent_registered == TRUE) {
        LOGD("Agent is already registered\n");
        return -1;
    }

    system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);

    if (error)
    {
        LOGE("errr:%s",error->message);
        g_error_free(error);

        return -1;
    }

    agent_capability = capability;

    agnet_interface = agent_org_bluez_agent1_skeleton_new ();

    g_signal_connect (agnet_interface,
                        "handle-release",
                        G_CALLBACK (on_handle_Release),
                        NULL);

    g_signal_connect (agnet_interface,
                        "handle-request-pin-code",
                        G_CALLBACK (on_handle_RequestPinCode),
                        NULL);

    g_signal_connect (agnet_interface,
                        "handle-display-pin-code",
                        G_CALLBACK (on_handle_DisplayPinCode),
                        NULL);

    g_signal_connect (agnet_interface,
                        "handle-request-passkey",
                        G_CALLBACK (on_handle_RequestPasskey),
                        NULL);

    g_signal_connect (agnet_interface,
                        "handle-display-passkey",
                        G_CALLBACK (on_handle_DisplayPasskey),
                        NULL);

    g_signal_connect (agnet_interface,
                        "handle-request-confirmation",
                        G_CALLBACK (on_handle_RequestConfirmation),
                        NULL);

    g_signal_connect (agnet_interface,
                        "handle-request-authorization",
                        G_CALLBACK (on_handle_RequestAuthorization),
                        NULL);

    g_signal_connect (agnet_interface,
                        "handle-authorize-service",
                        G_CALLBACK (on_handle_AuthorizeService),
                        NULL);

    g_signal_connect (agnet_interface,
                        "handle-cancel",
                        G_CALLBACK (on_handle_Cancel),
                        NULL);

    ret = g_dbus_interface_skeleton_export (
                                   G_DBUS_INTERFACE_SKELETON (agnet_interface),
                                    system_conn,
                                    AGENT_PATH,
                                    &error);

    if (FALSE == ret)
    {
        LOGE("errr:%s",error->message);
        g_error_free(error);
        g_object_unref(system_conn);

        return -1;
    }

    value = g_dbus_connection_call_sync(system_conn, BLUEZ_SERVICE,
                    AGENT_PATH,   AGENT_MANAGER_INTERFACE,
                    "RegisterAgent",    g_variant_new("(os)", AGENT_PATH,
                        agent_capability),
                    NULL,  G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT,
                    NULL, &error);

    if (NULL == value) {
        LOGE ("RegisterAgent Err: %s", error->message);
        g_error_free(error);

        g_dbus_interface_skeleton_unexport(
            G_DBUS_INTERFACE_SKELETON(agnet_interface));

        g_object_unref(system_conn);
        return -1;
    }

    g_variant_unref(value);

    agent_capability = NULL;

    agent_registered = TRUE;

    return 0;
}

#ifdef AGENT_THREAD
/*
 * agent thread function
 */
static void *agent_event_loop_thread(const char *capability)
{

    agentLoop = g_main_loop_new(NULL, FALSE);
    guint id;
    GError *error = NULL;
    gboolean ret;

    ret = create_and_register_agent(capability);

    if (0 == ret)
    {
        LOGD("g_main_loop_run\n");
        g_main_loop_run(agentLoop);


    }

    g_main_loop_unref(agentLoop);
    agentLoop = NULL;
    LOGD("exit...\n");
}
#endif


/* --- PUBLIC FUNCTIONS --- */


/*
 * start agent.
 * Returns: 0 - success or other errors
 */
int agent_register(const char *capability)
{
    int ret = 0;
    LOGD("\n");

    if (agent_registered == TRUE) {
        LOGD("Agent is already registered\n");
        return -1;
    }

#ifdef AGENT_THREAD
    pthread_t thread_id;
    pthread_create(&thread_id, NULL, agent_event_loop_thread, capability);
    pthread_setname_np(thread_id, "agent");

#else
    ret = create_and_register_agent(capability);
#endif

    return ret;
}

/*
 * stop agent.
 * Returns: 0 - success or other errors
 */
int stop_agent()
{
    GError *error = NULL;
    GVariant *value;

    LOGD("\n");

    if (agent_registered == FALSE) {
        LOGD("No agent is registered\n");
        return -1;
    }

    value = g_dbus_connection_call_sync(system_conn, BLUEZ_SERVICE,
                    AGENT_PATH,   AGENT_MANAGER_INTERFACE,
                    "UnregisterAgent",    g_variant_new("(o)", AGENT_PATH  ),
                    NULL,  G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT,
                    NULL, &error);

    if (NULL == value) {
        LOGE ("Error UnregisterAgent: %s", error->message);
        g_error_free(error);

        return -1;
    }

    g_dbus_interface_skeleton_unexport(
        G_DBUS_INTERFACE_SKELETON(agnet_interface));

    g_object_unref(agnet_interface);

    g_object_unref(system_conn);

    agent_registered = FALSE;

    memset(&agent_RegisterCallback, 0, sizeof(Agent_RegisterCallback_t));

#ifdef AGENT_THREAD
    if(agentLoop){
        g_main_loop_quit(agentLoop);
    }
#endif

    return 0;
}

/*
 * Register Agent Callback function
 */
void agent_API_register(const Agent_RegisterCallback_t* pstRegisterCallback)
{
    LOGD("\n");

    if (NULL != pstRegisterCallback)
    {
        if (NULL != pstRegisterCallback->agent_Release)
        {
            agent_RegisterCallback.agent_Release =
                pstRegisterCallback->agent_Release;
        }

        if (NULL != pstRegisterCallback->agent_RequestPinCode)
        {
            agent_RegisterCallback.agent_RequestPinCode =
                pstRegisterCallback->agent_RequestPinCode;
        }

        if (NULL != pstRegisterCallback->agent_DisplayPinCode)
        {
            agent_RegisterCallback.agent_DisplayPinCode =
                pstRegisterCallback->agent_DisplayPinCode;
        }

        if (NULL != pstRegisterCallback->agent_RequestPasskey)
        {
            agent_RegisterCallback.agent_RequestPasskey =
                pstRegisterCallback->agent_RequestPasskey;
        }

        if (NULL != pstRegisterCallback->agent_DisplayPasskey)
        {
            agent_RegisterCallback.agent_DisplayPasskey =
                pstRegisterCallback->agent_DisplayPasskey;
        }

        if (NULL != pstRegisterCallback->agent_RequestConfirmation)
        {
            agent_RegisterCallback.agent_RequestConfirmation =
                pstRegisterCallback->agent_RequestConfirmation;
        }

        if (NULL != pstRegisterCallback->agent_RequestAuthorization)
        {
            agent_RegisterCallback.agent_RequestAuthorization =
                pstRegisterCallback->agent_RequestAuthorization;
        }

        if (NULL != pstRegisterCallback->agent_AuthorizeService)
        {
            agent_RegisterCallback.agent_AuthorizeService =
                pstRegisterCallback->agent_AuthorizeService;
        }

        if (NULL != pstRegisterCallback->agent_Cancel)
        {
            agent_RegisterCallback.agent_Cancel =
                pstRegisterCallback->agent_Cancel;
        }

    }

}

/*
 * Send the agent event "RequestConfirmation" reply
 */
int agent_send_confirmation(gboolean confirmation)
{
    if (NULL == agent_event)
    {
        LOGW("Not agent event");
        return -1;
    }
    LOGD("%d-%d\n", confirmation, agent_event->type);

    if (REQUEST_CONFIRMATION != agent_event->type)
    {
        return -1;
    }

    if (TRUE == confirmation){
        agent_org_bluez_agent1_complete_request_confirmation(agent_event->object,
                                                      agent_event->invocation);
    }else{
        g_dbus_method_invocation_return_dbus_error (agent_event->invocation,
                                                    ERROR_BLUEZ_REJECT, "");
    }

    g_free(agent_event);
    agent_event = NULL;
    return 0;
}
/****************************** The End Of File ******************************/