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
#include "unit_device.h"

#include "gate/system/devices.h"
#include "gate/strings.h"
#include "gate/results.h"

#if !defined(GATE_SYS_DOS)


/********************
 *  device bthlist  *
 ********************/

typedef struct unit_device_bthlist_class
{
    gate_bool_t		connected;
} unit_device_bthlist_t;

static unit_device_bthlist_t device_bthlist_params = GATE_INIT_EMPTY;

static gatecli_paramter_t unit_device_bthlist_params[] = {
    { "--connected",			GATE_TYPE_BOOL,	&device_bthlist_params.connected, true }
};

static gate_bool_t unit_device_bthlist_cb(gate_device_bth_t const* bthdevice, void* param)
{
    gate_console_t* console = (gate_console_t*)param;
    gate_stream_t* stream = (gate_stream_t*)console;

    gate_stream_print_cstr(stream, "Name: ");
    gate_stream_println_cstr(stream, bthdevice->name);
    gate_stream_print_cstr(stream, "    Address: ");
    gate_stream_println_cstr(stream, bthdevice->address);

    if (bthdevice->connected)
    {
        gate_stream_print_cstr(stream, "    Connected");
    }
    else
    {
        gate_stream_print_cstr(stream, "    Not Connected");
    }
    if (bthdevice->authenticated)
    {
        gate_stream_print_cstr(stream, "    Authenticated");
    }
    else
    {
        gate_stream_print_cstr(stream, "    Not Authenticated");
    }
    gate_stream_println_cstr(stream, NULL);
    return true;
}

static gate_result_t unit_device_bthlist(gate_console_t* stream, char const* const* params, gate_size_t paramcount, gate_uint32_t flags)
{
    gate_result_t ret;

    ret = gate_device_bth_enum(&unit_device_bthlist_cb, stream);

    return ret;
}






/**********************
 *  device bthlelist  *
 **********************/

typedef struct unit_device_bthlelist_class
{
    gate_bool_t		connected;
} unit_device_bthlelist_t;

static unit_device_bthlelist_t device_bthlelist_params = GATE_INIT_EMPTY;

static gatecli_paramter_t unit_device_bthlelist_params[] = {
    { "--connected",			GATE_TYPE_BOOL,	&device_bthlelist_params.connected, true }
};

gate_bool_t unit_device_bthlelist_cb(gate_device_bthle_t const* bthledevice, void* param)
{
    gate_console_t* console = (gate_console_t*)param;
    gate_stream_t* stream = (gate_stream_t*)console;
    gate_size_t ndx;
    char guidbuffer[256];

    gate_stream_print_cstr(stream, "Name: ");
    gate_stream_println_cstr(stream, bthledevice->name);
    gate_stream_print_cstr(stream, "    Address: ");
    gate_stream_println_cstr(stream, bthledevice->address);
    gate_stream_print_cstr(stream, "    UID: ");
    gate_stream_println_cstr(stream, bthledevice->uid);

    for (ndx = 0; ndx != bthledevice->service_count; ++ndx)
    {
        gate_stream_print_cstr(stream, "    Service #");
        gate_stream_print_int(stream, (gate_int32_t)ndx);
        gate_stream_print_cstr(stream, ": ");
        gate_guid_to_string(&bthledevice->services[ndx].service_guid, guidbuffer);
        gate_stream_print_cstr(stream, guidbuffer);
        gate_stream_print_cstr(stream, ", ");
        gate_stream_print_int(stream, bthledevice->services[ndx].attribute_handle);
        gate_stream_println_cstr(stream, NULL);
    }

    gate_stream_println_cstr(stream, NULL);
    return true;
}

static gate_result_t unit_device_bthlelist(gate_console_t* stream, char const* const* params, gate_size_t paramcount, gate_uint32_t flags)
{
    gate_result_t ret;

    ret = gate_device_bthle_enum(&unit_device_bthlelist_cb, stream);


    return ret;
}





/*****************************
 *  device bthleshowservice  *
 *****************************/

typedef struct unit_device_bthleshowservice_class
{
    gate_string_t	uid;
    gate_string_t	address;
    gate_string_t	serviceguid;
} unit_device_bthleshowservice_t;

static unit_device_bthleshowservice_t device_bthleshowservice_params = GATE_INIT_EMPTY;

static gatecli_paramter_t unit_device_bthleshowservice_params[] = {
    { "--device-uid",		GATE_TYPE_STRING,	&device_bthleshowservice_params.uid,		true },
    { "--device-address",	GATE_TYPE_STRING,	&device_bthleshowservice_params.address,	true },
    { "--service-guid",		GATE_TYPE_STRING,	&device_bthleshowservice_params.serviceguid, false }
};

static gate_bool_t unit_device_bthle_showservice_cb(gate_device_bthle_characteristic_t const* chara, void* user_param)
{
    gate_stream_t* stream = (gate_stream_t*)user_param;
    gate_bool_t ret = true;
    char guidbuffer[64];
    char hexbuffer[1024];

    gate_guid_to_string(&chara->characteristic_guid, guidbuffer);

    gate_stream_print_cstr(stream, "Characteristic: ");
    gate_stream_print_cstr(stream, guidbuffer);
    gate_stream_println_cstr(stream, NULL);
    if (chara->data_length != 0)
    {
        gate_stream_print_cstr(stream, "    Value:  ");
        gate_str_print_hex_buffer(hexbuffer, sizeof(hexbuffer), chara->data, chara->data_length, true);
        gate_stream_println_cstr(stream, hexbuffer);
    }

    return ret;
}

static gate_result_t unit_device_bthleshowservice(gate_console_t* stream, char const* const* params, gate_size_t paramcount, gate_uint32_t flags)
{
    gate_result_t ret;
    gate_device_bthle_t device = GATE_INIT_EMPTY;
    gate_size_t ndx;
    gate_guid_t svcguid;
    gate_device_bthle_service_t* service = NULL;
    do
    {
        ret = gate_guid_parse_string(device_bthleshowservice_params.serviceguid.str, device_bthleshowservice_params.serviceguid.length, &svcguid);
        if (GATE_FAILED(ret))
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        if (!gate_string_is_empty(&device_bthleshowservice_params.uid))
        {
            ret = gate_device_bthle_get(&device_bthleshowservice_params.uid, &device);
            GATE_BREAK_IF_FAILED(ret);
        }
        else if (!gate_string_is_empty(&device_bthleshowservice_params.address))
        {
            ret = gate_device_bthle_get_by_address(&device_bthleshowservice_params.address, &device);
            GATE_BREAK_IF_FAILED(ret);
        }
        else
        {
            ret = GATE_RESULT_INVALIDARG;<--- Variable 'ret' is reassigned a value before the old one has been used.
        }


        for (ndx = 0; ndx != device.service_count; ++ndx)
        {
            if (gate_guid_equals(&device.services[ndx].service_guid, &svcguid))
            {
                service = &device.services[ndx];
                break;
            }
        }
        if (service == NULL)
        {
            ret = GATE_RESULT_NOMATCH;
            break;
        }

        ret = gate_device_bthle_service_enum(&device, service, &unit_device_bthle_showservice_cb, stream);<--- Variable 'ret' is reassigned a value before the old one has been used.

    } while (0);

    return ret;
}



/****************************
 *  device bthle-get-value  *
 ****************************/

typedef struct unit_device_bthlegetvalue_class
{
    gate_string_t	uid;
    gate_string_t	address;
    gate_string_t	serviceguid;
    gate_string_t	charaguid;
} unit_device_bthlegetvalue_t;

static unit_device_bthlegetvalue_t device_bthlegetvalue_params = GATE_INIT_EMPTY;

static gatecli_paramter_t unit_device_bthlegetvalue_params[] = {
    { "--device-uid",		GATE_TYPE_STRING,	&device_bthlegetvalue_params.uid,			true, "UID of BTLE device" },
    { "--device-address",	GATE_TYPE_STRING,	&device_bthlegetvalue_params.address,		true, "Address of BTLE device" },
    { "--service-guid",		GATE_TYPE_STRING,	&device_bthlegetvalue_params.serviceguid,	false, "GUID of BTLE device service to access" },
    { "--characteristic",	GATE_TYPE_STRING,	&device_bthlegetvalue_params.charaguid,		false, "GUID of BTLE characterist it read" }
};

static gate_result_t unit_device_bthlegetvalue(gate_console_t* stream, char const* const* params, gate_size_t paramcount, gate_uint32_t flags)
{
    gate_result_t ret;
    gate_stream_t* strm = (gate_stream_t*)stream;
    gate_device_bthle_t device;
    gate_guid_t svcguid;
    gate_guid_t charaguid;
    gate_device_bthle_service_t* service = NULL;
    char buffer[4096];
    gate_size_t buffer_len = sizeof(buffer);
    char hexbuffer[8192];

    do
    {
        ret = gate_guid_parse_string(device_bthlegetvalue_params.serviceguid.str, device_bthlegetvalue_params.serviceguid.length, &svcguid);
        if (GATE_FAILED(ret))
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        ret = gate_guid_parse_string(device_bthlegetvalue_params.charaguid.str, device_bthlegetvalue_params.charaguid.length, &charaguid);
        if (GATE_FAILED(ret))
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        if (!gate_string_is_empty(&device_bthlegetvalue_params.uid))
        {
            ret = gate_device_bthle_get(&device_bthlegetvalue_params.uid, &device);
            GATE_BREAK_IF_FAILED(ret);
        }
        else if (!gate_string_is_empty(&device_bthlegetvalue_params.address))
        {
            ret = gate_device_bthle_get_by_address(&device_bthlegetvalue_params.address, &device);
            GATE_BREAK_IF_FAILED(ret);
        }
        else
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        ret = gate_device_bthle_characteristic_get(&device, &svcguid, &charaguid, buffer, &buffer_len);
        GATE_BREAK_IF_FAILED(ret);

        gate_str_print_hex_buffer(hexbuffer, sizeof(hexbuffer), buffer, buffer_len, true);
        gate_stream_println_cstr(strm, hexbuffer);
    } while (0);

    return ret;
}





/****************************
 *  device bthle-set-value  *
 ****************************/

typedef struct unit_device_bthlesetvalue_class
{
    gate_string_t	uid;
    gate_string_t	address;
    gate_string_t	serviceguid;
    gate_string_t	charaguid;
    gate_string_t	value;
} unit_device_bthlesetvalue_t;

static unit_device_bthlesetvalue_t device_bthlesetvalue_params = GATE_INIT_EMPTY;

static gatecli_paramter_t unit_device_bthlesetvalue_params[] = {
    { "--device-uid",		GATE_TYPE_STRING,	&device_bthlesetvalue_params.uid, true, "UID of BTLE device" },
    { "--device-address",	GATE_TYPE_STRING,	&device_bthlesetvalue_params.address, true, "Address of BTLE device" },
    { "--service-guid",		GATE_TYPE_STRING,	&device_bthlesetvalue_params.serviceguid, false, "GUID of BTLE device service to access" },
    { "--characteristic",	GATE_TYPE_STRING,	&device_bthlesetvalue_params.charaguid, false, "GUID of BTLE characterist it read" },
    { "--value",			GATE_TYPE_STRING,	&device_bthlesetvalue_params.value, false, "GUID of BTLE characterist it read" }
};

static gate_result_t unit_device_bthlesetvalue(gate_console_t* stream, char const* const* params, gate_size_t paramcount, gate_uint32_t flags)
{
    gate_result_t ret;
    gate_stream_t* strm = (gate_stream_t*)stream;
    gate_device_bthle_t device;
    gate_guid_t svcguid;
    gate_guid_t charaguid;
    gate_device_bthle_service_t* service = NULL;
    char buffer[4096];
    gate_size_t buffer_len = sizeof(buffer);<--- Variable 'buffer_len' is reassigned a value before the old one has been used.

    do
    {
        ret = gate_guid_parse_string(device_bthlesetvalue_params.serviceguid.str, device_bthlesetvalue_params.serviceguid.length, &svcguid);
        if (GATE_FAILED(ret))
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        ret = gate_guid_parse_string(device_bthlesetvalue_params.charaguid.str, device_bthlesetvalue_params.charaguid.length, &charaguid);
        if (GATE_FAILED(ret))
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        buffer_len = gate_str_parse_hex_buffer(device_bthlesetvalue_params.value.str, device_bthlesetvalue_params.value.length, buffer, sizeof(buffer));<--- Variable 'buffer_len' is reassigned a value before the old one has been used.

        if (!gate_string_is_empty(&device_bthlesetvalue_params.uid))
        {
            ret = gate_device_bthle_get(&device_bthlesetvalue_params.uid, &device);
            GATE_BREAK_IF_FAILED(ret);
        }
        else if (!gate_string_is_empty(&device_bthlesetvalue_params.address))
        {
            ret = gate_device_bthle_get_by_address(&device_bthlesetvalue_params.address, &device);
            GATE_BREAK_IF_FAILED(ret);
        }
        else
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        ret = gate_device_bthle_characteristic_set(&device, &svcguid, &charaguid, buffer, buffer_len);
        GATE_BREAK_IF_FAILED(ret);

    } while (0);

    return ret;
}





gatecli_function_t device_functions[] =
{
    GATECLI_FUNCTION_PARAMS("bth-list",				unit_device_bthlist,			unit_device_bthlist_params, "Lists remote Bluetooth devices"),
    GATECLI_FUNCTION_PARAMS("bthle-list",			unit_device_bthlelist,			unit_device_bthlelist_params, "Lists remote Bluetooth LE devices"),
    GATECLI_FUNCTION_PARAMS("bthle-showservice",	unit_device_bthleshowservice,	unit_device_bthleshowservice_params,"Shows service characteristics of a Bluetooth LE device"),
    GATECLI_FUNCTION_PARAMS("bthle-get-value",		unit_device_bthlegetvalue,		unit_device_bthlegetvalue_params, "Reads the value from a Bluetooth LE characteristic"),
    GATECLI_FUNCTION_PARAMS("bthle-set-value",		unit_device_bthlesetvalue,		unit_device_bthlesetvalue_params, "Writes a new value to a Bluetooth LE characteristic")
};

gatecli_unit_t global_unit_device =
{
    "device",
    "System device functions and features",
    &device_functions[0],
    sizeof(device_functions) / sizeof(device_functions[0])
};

gatecli_unit_t* unit_device()
{
    return &global_unit_device;
}

#endif