summaryrefslogtreecommitdiffstats
path: root/ofono-client.c
blob: 26b28c790577188ff5c0dba8013aa2c8d2a30f4d (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
/*  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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>

#include "lib_ofono.h"
#include "lib_ofono_modem.h"
#include "ofono-client.h"

#ifdef OFONO_THREAD
static GMainLoop *OfonoLoop = NULL;
#endif

static stOfonoManager OfonoManager = { 0 };
static Ofono_RegisterCallback_t ofono_RegisterCallback = { 0 };


/* ------ LOCAL  FUNCTIONS --------- */
static OFONOMODEMOrgOfonoModem* modem_create_proxy(struct ofono_modem *modem);


/*
 * make a copy of each element
 * And, to entirely free the new btd_device, you could do: modem_free
 */
struct ofono_modem *modem_copy(struct ofono_modem* modem)
{
    struct ofono_modem * temp;

    if (NULL == modem) {
        return NULL;
    }

    temp = g_malloc0(sizeof(struct ofono_modem));
    temp->path = g_strdup(modem->path);

    temp->proxy = g_object_ref (modem->proxy);

    temp->powered = modem->powered;

    return temp;
}

/*
 * Frees all of the memory
 */
static void modem_free(struct ofono_modem* modem)
{

    if (NULL == modem) {
        return ;
    }

    if (modem->path) {
        g_free(modem->path);
        modem->path = NULL;
    }

    if (modem->proxy){
        g_object_unref(modem->proxy);
        modem->proxy = NULL;
    }

    g_free(modem);

}

#if 0
//debug print
void modem_print(struct ofono_modem *modem)
{
    gchar *s;
    g_print("device %p\n",modem);
    g_print("path\t\t:%s\n",modem->path);
    g_print("powered\t\t:%d\n",modem->powered);

}
#endif

static int modem_path_cmp(struct ofono_modem * modem, const gchar* path )
{
    return g_strcmp0 (modem->path, path);
}

static void modem_list_lock(void)
{
    g_mutex_lock(&(OfonoManager.m));
}

static void modem_list_unlock(void)
{
    g_mutex_unlock(&(OfonoManager.m));
}

#if 0
//debug print
void modem_list_print()
{
    GSList * temp = OfonoManager.modem;
    while (temp) {
        struct ofono_modem *modem = temp->data;
        temp = temp->next;
        g_print("----------------------------------------\n");
        modem_print(modem);
    }
    g_print("----------------------------------------\n");
}
#endif


/*
 * remove all the devices
*/
static void modem_list_cleanup()
{
    LOGD("\n");
    GSList * temp = OfonoManager.modem;
    while (temp) {
        struct ofono_modem *modem = temp->data;
        temp = temp->next;

        OfonoManager.modem = g_slist_remove_all(OfonoManager.modem,
                modem);

        modem_free(modem);
    }
}

/*
 * search ofono modem by path
 * Returns the first found btd_device or NULL if it is not found
 */
static  struct ofono_modem *modem_list_find_modem_by_path(const char* path)
{
    //LOGD("path%s\n",path);
    GSList * temp = NULL;

    temp = g_slist_find_custom (OfonoManager.modem, path,
                                (GCompareFunc)modem_path_cmp);

    if (temp) {
        return temp->data;
    }

    return NULL;
}

static void on_modem_property_changed (OFONOMODEMOrgOfonoModem* object,
                                        gchar* property,
                                        GVariant *value,
                                        gpointer userdata)
{
#ifdef _DEBUG_PRINT_DBUS
    gchar *s;

    g_print ("%s\n",property);
    s = g_variant_print (value, TRUE);
    g_print ("  %s\n",  s);
    g_free (s);
#endif

    struct ofono_modem *modem;


    if (NULL==property || NULL==value || NULL==userdata){
        LOGD(" receive null data\n");
        return;
    }

    modem = userdata;

    if (0 == g_strcmp0(property, "Powered"))
    {

        GVariant *temp = g_variant_get_variant (value);
        //g_print ("update\n");
        gboolean new_value;
        //gboolean old_value;
        g_variant_get(temp, "b", &new_value );
        g_variant_unref(temp);
        LOGD("Powered %d\n",new_value);
        //old_value = modem->powered;
        modem->powered = new_value;

        if (NULL != ofono_RegisterCallback.modem_propertyies_changed)
        {
            ofono_RegisterCallback.modem_propertyies_changed(modem);
        }
    }


}

static void on_modem_added (OFONOOrgOfonoManager* object,
                            gchar* path,
                            GVariant *value,
                            gpointer userdata)
{
#ifdef _DEBUG_PRINT_DBUS
    gchar *s;

    g_print ("on_modom_added\n");
    g_print ("%s\n",path);
    s = g_variant_print (value, TRUE);
    g_print ("  %s\n",  s);
    g_free (s);
#endif


    GError *error = NULL;

    if (NULL == path)
        return;

    struct ofono_modem *modem = g_malloc0(sizeof(struct ofono_modem));

    LOGD("new modem path:%s,%p\n", path, modem);

    modem->path = g_strdup(path);
    modem->proxy = modem_create_proxy(modem);



    GVariant *property_value = NULL;
    g_variant_lookup(value, "Powered",
                     "*", &property_value);

    if (property_value)
    {
        gboolean bValue = FALSE;
        g_variant_get(property_value, "b", &bValue );
        modem->powered = bValue;

        g_variant_unref (property_value);
    }

    modem_list_lock();
    OfonoManager.modem = g_slist_prepend(OfonoManager.modem, modem);
    modem_list_unlock();

    if (NULL != ofono_RegisterCallback.modem_added){
        ofono_RegisterCallback.modem_added(modem);
    }
}

static void on_modem_removed (OFONOOrgOfonoManager * object,
                                gchar* path,
                                gpointer userdata)
{
#ifdef _DEBUG_PRINT_DBUS
    gchar *s;
    g_print ("on_modem_removed\n");
    g_print ("%s\n",path);
#endif

    struct ofono_modem *modem = NULL;

    modem_list_lock();

    modem = modem_list_find_modem_by_path(path);

    LOGD("remove modem path:%s,%p\n", path, modem);

    if (modem){

        OfonoManager.modem = g_slist_remove_all(OfonoManager.modem,
                                    modem);

        if (NULL != ofono_RegisterCallback.modem_removed)
        {
            ofono_RegisterCallback.modem_removed(modem);
        }

        modem_free(modem);
    }

    modem_list_unlock();

}


static OFONOMODEMOrgOfonoModem* modem_create_proxy(struct ofono_modem *modem)
{
    GError *error = NULL;

    if (NULL == modem)
    {
        return NULL;
    }

    if (NULL == modem->path)
    {
        return NULL;
    }

    modem->proxy = ofono_modem_org_ofono_modem_proxy_new_for_bus_sync (
        G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, OFONO_SERVICE,
        modem->path, NULL, &error);

    if (error)
    {
        LOGW ("Error : %s\n", error->message);
        g_error_free(error);
        return NULL;
    }

    g_signal_connect (modem->proxy,
                      "property_changed",
                      G_CALLBACK (on_modem_property_changed),
                      modem);

    return modem->proxy;

}


/*
 * Force Update the modem list
 * Call <method>GetModems
 * Returns: 0 - success or other errors
 */
int modem_list_update() {
    LOGD("\n");

    GError *error = NULL;
    GVariant *result = NULL;
    GSList *newDeviceList = NULL;


    result = g_dbus_connection_call_sync(OfonoManager.system_conn,
            OFONO_SERVICE, OFONO_MANAGER_PATH, OFONO_MANAGER_INTERFACE,
            "GetModems", NULL, NULL,
            G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error);

    if (error) {
        LOGW ("Error : %s\n", error->message);
        g_error_free(error);
        return -1;
    }

    GVariant *ArrayValue = NULL;
    GVariantIter *ArrayValueIter;
    GVariant *Value = NULL;

    g_variant_get(result, "(*)", &ArrayValue);

    g_variant_get(ArrayValue, "a*", &ArrayValueIter);
    while (g_variant_iter_loop(ArrayValueIter, "*", &Value)) {

        GVariantIter dbus_object_iter;
        GVariant *dbusObjecPath;
        GVariant *dbusObjecProperties;

        gchar *pObjecPath = NULL;
        struct ofono_modem *modem = NULL;

        g_variant_iter_init(&dbus_object_iter, Value);

        //1st : DBus Object Path
        dbusObjecPath = g_variant_iter_next_value(&dbus_object_iter);

        g_variant_get(dbusObjecPath, "o", &pObjecPath);

        LOGD("object path %s\n",pObjecPath);

          //ObjectPath is /hfp/org/bluez/hci0/dev_xx_xx_xx_xx_xx_xx
        if ((41 != strlen(pObjecPath))
            || (NULL == g_strstr_len(pObjecPath, 23,
                                      "/hfp"ADAPTER_PATH"/dev"))) {
            g_free(pObjecPath);
            pObjecPath = NULL;
            g_variant_unref(dbusObjecPath);
            continue;
        }

        modem = g_malloc0(sizeof(struct ofono_modem));
        modem->path = g_strdup(pObjecPath);
        g_free(pObjecPath);
        pObjecPath = NULL;
        g_variant_unref(dbusObjecPath);

        LOGD("Found new device%s\n",modem->path );

        modem->proxy = modem_create_proxy(modem);

        //2nd : DBus Interfaces under Object Path
        dbusObjecProperties = g_variant_iter_next_value(&dbus_object_iter);

        GVariant *property_value = NULL;
        g_variant_lookup(dbusObjecProperties, "Powered",
                         "*", &property_value);

        if (property_value)
        {
            gboolean bValue = FALSE;
            g_variant_get(property_value, "b", &bValue );
            modem->powered = bValue;

            g_variant_unref (property_value);
        }

        g_variant_unref(dbusObjecProperties);

        //Save device to newDeviceList
        newDeviceList = g_slist_prepend(newDeviceList, modem);

    }

    g_variant_iter_free(ArrayValueIter);
    g_variant_unref(ArrayValue);

    g_variant_unref(result);


    //Force update device, so clean first
    modem_list_lock();

    modem_list_cleanup();

    OfonoManager.modem = newDeviceList;

    modem_list_unlock();

    return 0;
}


/*
 * init cli dbus connection
 * Returns: 0 - success or other errors
 */
static int ofono_manager_connect_to_dbus(void)
{
    GError *error = NULL;

    OfonoManager.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;
    }

    OfonoManager.proxy = ofono_org_ofono_manager_proxy_new_for_bus_sync(
        G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, OFONO_SERVICE,
        OFONO_MANAGER_PATH, NULL, &error);

    if (error) {
        LOGW("Create Ofono manager client fail\n");
        LOGW("Error:%s\n", error->message);
        g_error_free(error);

        g_object_unref(OfonoManager.system_conn);
        return -1;
    }

    return 0;
}

/*
 * register callback function
 * Returns: 0 - success or other errors
 */
static int ofono_manager_register_callback(void)
{

    g_signal_connect (OfonoManager.proxy,
                      "modem-added",
                      G_CALLBACK (on_modem_added),
                      NULL);

    g_signal_connect (OfonoManager.proxy,
                      "modem-removed",
                      G_CALLBACK (on_modem_removed),
                      NULL);

    return 0;
}

/*
 * init dbus and register callback
 */
static int ofono_mamager_init(void)
{
    int ret = 0;
    LOGD("\n");
    ret = ofono_manager_connect_to_dbus();

    if (0 != ret){
        LOGW("init fail\n");
        return -1;
    }

    g_mutex_init(&(OfonoManager.m));

    ofono_manager_register_callback();
    modem_list_update();

    OfonoManager.inited = TRUE;

    return 0;
}

#ifdef AGENT_THREAD
/*
 * Ofono Manager Thread
 * register callback function and create a new GMainLoop structure
 */
static void *ofono_event_loop_thread()
{
    int ret = 0;

    OfonoLoop = g_main_loop_new(NULL, FALSE);;

    ret = ofono_mamager_init();

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

    }

    g_main_loop_unref(OfonoLoop);
    LOGD("exit...\n");
}
#endif

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

/*
 * Get the ofono modem "Powered" property
 */
gboolean getOfonoModemPoweredByPath (gchar* path)
{
    struct ofono_modem *modem = NULL;
    gboolean powered = FALSE;

    modem_list_lock();

    modem = modem_list_find_modem_by_path(path);

    if (modem){
        powered = modem->powered;
    }

    LOGD("get modem %p by path:%s,%d\n", modem, path, powered);

    modem_list_unlock();

    return powered;
}


/*
 * Init the ofono manager
 */
int OfonoManagerInit(void)
{
    int ret = 0;

    LOGD("\n");

    if (TRUE == OfonoManager.inited)
    {
        LOGW("Ofono Manager is already inited\n");
        return -1;

    }

#ifdef OFONO_THREAD
    pthread_t thread_id;

    pthread_create(&thread_id, NULL, ofono_event_loop_thread, NULL);
    pthread_setname_np(thread_id, "ofono_manager");

#else

    ret = ofono_mamager_init();

#endif

    return ret;
}

/*
 * Quit the ofono manager
 */
int OfonoManagerQuit(void)
{
    LOGD("\n");

    if (TRUE != OfonoManager.inited)
    {
        LOGW("Ofono Manager is not inited\n");
        return -1;

    }
#ifdef OFONO_THREAD
    g_main_loop_quit(OfonoLoop);
#endif

    memset(&ofono_RegisterCallback, 0, sizeof(Ofono_RegisterCallback_t));

    g_object_unref(OfonoManager.proxy);

    modem_list_lock();
    modem_list_cleanup();
    modem_list_unlock();

    g_mutex_clear (&(OfonoManager.m));

    g_object_unref(OfonoManager.system_conn);

    OfonoManager.inited = FALSE;

    return 0;

}

/*
 * Register ofono Callback function
 */
void OfonoModemAPIRegister(const Ofono_RegisterCallback_t* pstRegisterCallback)
{
    LOGD("\n");

    if (NULL != pstRegisterCallback)
    {
        if (NULL != pstRegisterCallback->modem_added)
        {
            ofono_RegisterCallback.modem_added =
                pstRegisterCallback->modem_added;
        }

        if (NULL != pstRegisterCallback->modem_removed)
        {
            ofono_RegisterCallback.modem_removed =
                pstRegisterCallback->modem_removed;
        }

        if (NULL != pstRegisterCallback->modem_propertyies_changed)
        {
            ofono_RegisterCallback.modem_propertyies_changed =
                pstRegisterCallback->modem_propertyies_changed;
        }

    }

}