WXK
2024-12-20 51135221cd73a2b3a6ce4b5ec906396d5a33b4c7
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
/**************************************************************************//**
 * @file     main.c
 * @version  V1.0.0
 *
 * @brief    main.c
 *
 * @note
 * Copyright (c) 2022 Shanghai Panchip Microelectronics Co.,Ltd.
 *
 ******************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
 
#include "PANSeries.h"
 
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
 
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
#include "console/console.h"
#include "services/gap/ble_svc_gap.h"
#include "image_map_config.h"
 
#if defined(NIMBLE_SPARK_SUP)
#include "nimble/pan107x/nimble_glue_spark.h"
#else
#include "nimble/pan107x/nimble_glue.h"
#endif
 
#define MODLOG_DFLT printf
 
/**API set public address */
extern uint32_t db_set_bd_address(uint8_t *bd_addr);
 
static const char *adv_name = "HX_Beacon";
 
#define COPMANY_ID    0xD1, 0x07 /* Panchip */
#define PACKET_ID    0x01
#define DEVICE_TYPE    0x20 /* Device type */
#define DF_FIELD    0x67, 0xF7, 0xDB, 0x34, 0xC4, 0x03, 0x8E, 0x5C, 0x0B, 0xAA, 0x97, 0x30, 0x56, 0xE6
 
/* Advertising data */
static uint8_t manuf_data[] = {
    COPMANY_ID,
    PACKET_ID,
    DEVICE_TYPE,
    /* Carries information of Tag's TX rate, TX power and ID type */
    0x15,
 
    /* Tag ID */
    0x00, 0x00, 0x00, 0x00, 0x10, 0x05,
//    0x11, 0x22,
    /* Checksum */
    0xC9,
 
    DF_FIELD
};
 
static int bleprph_gap_event(struct ble_gap_event *event, void *arg);
#if CONFIG_EXAMPLE_RANDOM_ADDR
static uint8_t own_addr_type = BLE_OWN_ADDR_RPA_RANDOM_DEFAULT;
#else
static uint8_t own_addr_type;
#endif
 
#if MYNEWT_VAL(BLE_POWER_CONTROL)
static struct ble_gap_event_listener power_control_event_listener;
#endif
 
static void print_addr(uint8_t* addr)
{
    for(uint8_t i=0; i<6; i++)
    {
        printf("%02x ", addr[i]);
    }
    printf("\n");
}
 
 
/**
 * Enables advertising with the following parameters:
 *     o General discoverable mode.
 *     o Undirected connectable mode.
 */
static void ble_cte_beacon_advertise(void)
{
    struct ble_gap_adv_params adv_params;
    struct ble_hs_adv_fields fields;
 
    const char *name;
    int rc;
 
    /**
     *  Set the advertisement data included in our advertisements:
     *     o Flags (indicates advertisement type and other general info).
     *     o Advertising tx power.
     *     o Device name.
     *     o 16-bit service UUIDs (alert notifications).
     */
 
    memset(&fields, 0, sizeof fields);
 
    /* Advertise two flags:
     *     o Discoverability in forthcoming advertisement (general)
     *     o BLE-only (BR/EDR unsupported).
     */
    fields.flags = BLE_HS_ADV_F_DISC_GEN |
                   BLE_HS_ADV_F_BREDR_UNSUP;
 
    fields.mfg_data = manuf_data;
    fields.mfg_data_len = sizeof(manuf_data);
 
    rc = ble_gap_adv_set_fields(&fields);
    if (rc != 0) {
        MODLOG_DFLT("error setting advertisement data; rc=%d\n", rc);
        return;
    }
 
    memset(&fields, 0, sizeof fields);
    name = ble_svc_gap_device_name();
    fields.name = (uint8_t *)name;
    fields.name_len = strlen(name);
    fields.name_is_complete = 1;
    rc = ble_gap_adv_rsp_set_fields(&fields);
    if (rc != 0) {
        MODLOG_DFLT("error setting advertisement rsp data; rc=%d\n", rc);
        return;
    }
 
    /* Begin advertising. */
    memset(&adv_params, 0, sizeof adv_params);
    adv_params.conn_mode = BLE_GAP_CONN_MODE_NON;
    adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
    
    adv_params.itvl_min = BLE_GAP_ADV_ITVL_MS(500);
    adv_params.itvl_max = BLE_GAP_ADV_ITVL_MS(500);
    
    rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER,
                           &adv_params, bleprph_gap_event, NULL);
    if (rc != 0) {
        MODLOG_DFLT("error enabling advertisement; rc=%d\n", rc);
        return;
    }
 
    MODLOG_DFLT("adv start successfully\n");
}
 
 
#if MYNEWT_VAL(BLE_POWER_CONTROL)
static void bleprph_power_control(uint16_t conn_handle)
{
    int rc;
 
    rc = ble_gap_read_remote_transmit_power_level(conn_handle, 0x01 );  // Attempting on LE 1M phy
    assert (rc == 0);
 
    rc = ble_gap_set_transmit_power_reporting_enable(conn_handle, 0x1, 0x1);
    assert (rc == 0);
}
#endif
 
 
#if MYNEWT_VAL(BLE_POWER_CONTROL)
static int
bleprph_gap_power_event(struct ble_gap_event *event, void *arg)
{
 
    switch(event->type) {
    case BLE_GAP_EVENT_TRANSMIT_POWER:
    MODLOG_DFLT("Transmit power event : status=%d conn_handle=%d reason=%d "
                          "phy=%d power_level=%x power_level_flag=%d delta=%d",
            event->transmit_power.status,
            event->transmit_power.conn_handle,
            event->transmit_power.reason,
            event->transmit_power.phy,
            event->transmit_power.transmit_power_level,
            event->transmit_power.transmit_power_level_flag,
            event->transmit_power.delta);
    return 0;
 
    case BLE_GAP_EVENT_PATHLOSS_THRESHOLD:
    MODLOG_DFLT("Pathloss threshold event : conn_handle=%d current path loss=%d "
                          "zone_entered =%d",
            event->pathloss_threshold.conn_handle,
            event->pathloss_threshold.current_path_loss,
            event->pathloss_threshold.zone_entered);
    return 0;
 
    default:
    return 0;
    }
}
#endif
 
 
/**
 * The nimble host executes this callback when a GAP event occurs.  The
 * application associates a GAP event callback with each connection that forms.
 * bleprph uses the same callback for all connections.
 *
 * @param event                 The type of event being signalled.
 * @param ctxt                  Various information pertaining to the event.
 * @param arg                   Application-specified argument; unused by
 *                                  bleprph.
 *
 * @return                      0 if the application successfully handled the
 *                                  event; nonzero on failure.  The semantics
 *                                  of the return code is specific to the
 *                                  particular GAP event being signalled.
 */
static int bleprph_gap_event(struct ble_gap_event *event, void *arg)
{
    switch (event->type) {
 
    case BLE_GAP_EVENT_ADV_COMPLETE:
        MODLOG_DFLT("advertise complete; reason=%d",
                    event->adv_complete.reason);
        ble_cte_beacon_advertise();
        return 0;
    default:
        return 0;
    }
}
 
static void bleprph_on_reset(int reason)
{
    MODLOG_DFLT("Resetting state; reason=%d\n", reason);
}
 
#if CONFIG_EXAMPLE_RANDOM_ADDR
static void
ble_app_set_addr(void)
{
    ble_addr_t addr;
    int rc;
 
    /* generate new non-resolvable private address */
    rc = ble_hs_id_gen_rnd(0, &addr);
    assert(rc == 0);
 
    /* set generated address */
    rc = ble_hs_id_set_rnd(addr.val);
 
    assert(rc == 0);
}
#endif
 
static void bleprph_on_sync(void)
{
    int rc;
 
#if CONFIG_EXAMPLE_RANDOM_ADDR
    /* Generate a non-resolvable private address. */
    ble_app_set_addr();
#endif
 
    /* Make sure we have proper identity address set (public preferred) */
#if CONFIG_EXAMPLE_RANDOM_ADDR
    rc = ble_hs_util_ensure_addr(1);
#else
    rc = ble_hs_util_ensure_addr(0);
#endif
    assert(rc == 0);
 
    /* Figure out address to use while advertising (no privacy for now) */
    rc = ble_hs_id_infer_auto(0, &own_addr_type);
    if (rc != 0) {
        MODLOG_DFLT("error determining address type; rc=%d\n", rc);
        return;
    }
 
    /* Printing ADDR */
    uint8_t addr_val[6] = {0};
    rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
 
    MODLOG_DFLT("Device Address: ");
    print_addr(addr_val);
    MODLOG_DFLT("\n");
    /* Begin advertising. */
    ble_cte_beacon_advertise();
}
 
static void print_version_info(void)
{
    #include "app_version.h"
    struct smp_image_header *image_header_p = (void *)SIZE_BOOTLOADER;
 
    printf("APP version: %d.%d.%d\n", image_header_p->ih_ver.iv_major, image_header_p->ih_ver.iv_minor, image_header_p->ih_ver.iv_revision);
}
 
 
 
void app_main(void)
{
    int rc;
 
    print_version_info();
 
    /** set public address*/
    uint8_t pub_mac[6]={8,2,3,4,5,6};
 
    #if CONFIG_USER_CHIP_MAC_ADDR
    extern uint8_t pan10x_mac_addr_get(uint8_t *mac);
    pan10x_mac_addr_get(pub_mac);
    #endif
 
 
    db_set_bd_address(pub_mac);
 
    /* Initialize the NimBLE host configuration. */
    ble_hs_cfg.reset_cb = bleprph_on_reset;
    ble_hs_cfg.sync_cb = bleprph_on_sync;
 
    /* Set the default device name. */
    rc = ble_svc_gap_device_name_set(adv_name);
    assert(rc == 0);
 
    hs_thread_init();
}
 
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
{
    printf("task overflow:%s\r\n", pcTaskName);
    while(1);
}