ble.c 9.49 KB
Newer Older
1
2
3
4
5
6
7
#include "wsf_types.h"
#include "wsf_os.h"
#include "wsf_buf.h"
#include "wsf_timer.h"
#include "wsf_trace.h"
#include "app_ui.h"
#include "fit/fit_api.h"
8
#include "hci_vs.h"
9

10
11
12
13
14
15
#include "att_defs.h"
#include "util/bstream.h"
#include "att_api.h"

#include "modules.h"

16
17
18
#include "FreeRTOS.h"
#include "timers.h"

19
20
#include <stdio.h>
#include <string.h>
21
22
23
24
25
26
27
28
29
30
#include <stdbool.h>


/* Size of buffer for stdio functions */
#define WSF_BUF_POOLS       6
#define WSF_BUF_SIZE        0x1048

uint32_t SystemHeapSize=WSF_BUF_SIZE;
uint32_t SystemHeap[WSF_BUF_SIZE/4];
uint32_t SystemHeapStart;
31

32
33
34
/* Task ID for the ble handler */
static TaskHandle_t ble_task_id = NULL;

35
36
37
38
39
40
41
42
/*! Default pool descriptor. */
static wsfBufPoolDesc_t mainPoolDesc[WSF_BUF_POOLS] =
{
  {  16,  8 },
  {  32,  4 },
  {  64,  4 },
  { 128,  4 },
  { 256,  4 },
43
  { 512,  4 }
44
45
};

46
47
/*! \brief  Stack initialization for app. */
extern void StackInitFit(void);
48

49
50
/*************************************************************************************************/
void PalSysAssertTrap(void)
51
{
52
    while(1) {}
53
54
}

55
56
57

/*************************************************************************************************/
static bool_t myTrace(const uint8_t *pBuf, uint32_t len)
58
59
60
61
62
{
    extern uint8_t wsfCsNesting;

    if (wsfCsNesting == 0)
    {
63
64
        fwrite(pBuf, len, 1, stdout);
        return TRUE;
65
    }
66
67

    return FALSE;
68
69
}

70
71
72
73
74
75
76
/*************************************************************************************************/
/*!
 *  \brief  Initialize WSF.
 *
 *  \return None.
 */
/*************************************************************************************************/
77
78
static void WsfInit(void)
{
79
    uint32_t bytesUsed;
80
    WsfTimerInit();
81
82
83
84
85
86
87
88
89
90

    SystemHeapStart = (uint32_t)&SystemHeap;
    memset(SystemHeap, 0, sizeof(SystemHeap));
    //printf("SystemHeapStart = 0x%x\n", SystemHeapStart);
    //printf("SystemHeapSize = 0x%x\n", SystemHeapSize);
    bytesUsed = WsfBufInit(WSF_BUF_POOLS, mainPoolDesc);
    printf("bytesUsed = %u\n", (unsigned int)bytesUsed);
    
    WsfTraceRegisterHandler(myTrace);
    WsfTraceEnable(TRUE);
91
}
92
/* TODO: We need a source of MACs */
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * In two-chip solutions, setting the address must wait until the HCI interface is initialized.
 * This handler can also catch other Application events, but none are currently implemented.
 * See ble-profiles/sources/apps/app/common/app_ui.c for further details.
 *
 */
void SetAddress(uint8_t event)
{
    uint8_t bdAddr[6] = {0x02, 0x02, 0x44, 0x8B, 0x05, 0x00};

    switch (event) {
    case APP_UI_RESET_CMPL:
        printf("Setting address -- MAC %02X:%02X:%02X:%02X:%02X:%02X\n", bdAddr[5], bdAddr[4], bdAddr[3], bdAddr[2], bdAddr[1], bdAddr[0]);
106
        HciVsSetBdAddr(bdAddr);
107
108
109
110
111
112
        break;
    default:
        break;
    }
}

113
114
115
116
117
118
static StaticTimer_t x;
TimerHandle_t timerWakeup = NULL;
int lasttick = 0;
bool schedule_needed = false;

static void vTimerCallback(xTimerHandle pxTimer)
119
{
120
121
122
123
124
125
    //printf("wake\n");
    int tick = xTaskGetTickCount();
    printf("WsfTimerUpdate(%d)\n", tick - lasttick);
    WsfTimerUpdate(tick - lasttick);
    lasttick = tick;
    //printf("done\n");
126
127
}

128
129
130
131
132
133
134
135
136
137
static void notify(void)
{
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    if(xPortIsInsideInterrupt()) {
	    vTaskNotifyGiveFromISR(ble_task_id, &xHigherPriorityTaskWoken);
        portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
    } else {
	     xTaskNotifyGive(ble_task_id);
    }
}
138

139
140
141
142
143
void WsfTimerNotify(void)
{
    //printf("WsfTimerNotify\n");
    notify();
}
144

145
146
147
148
149
void wsf_ble_signal_event(void)
{
    //printf("wsf_ble_signal_event\n");
    notify();
}
150

151
static void scheduleTimer(void)
152
153
154
155
{
    bool_t          timerRunning;
    wsfTimerTicks_t time_to_next_expire;

156
    time_to_next_expire = WsfTimerNextExpiration(&timerRunning);
157
158

    if(timerRunning) {
159
160
        //printf("time_to_next_expire = %d\n", time_to_next_expire);
        //printf("change period\n");
161
        if(timerWakeup != NULL) {
162
163
            xTimerChangePeriod(timerWakeup, pdMS_TO_TICKS(time_to_next_expire), 0);
            //printf("insert done\n");
164
165
166
167
168
169
170
        } else {
            printf("could not create timer\n");
        }
    } else {
        printf("No timer running\n");
    }
}
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
#define UART_START_HDL                    0x800                /*!< \brief Service start handle. */
#define UART_END_HDL                      (BATT_MAX_HDL - 1)  /*!< \brief Service end handle. */

/**************************************************************************************************
 Handles
**************************************************************************************************/

/*! \brief UART Service Handles */
enum
{
  UART_SVC_HDL = UART_START_HDL,        /*!< \brief UART service declaration */
  UART_RX_CH_HDL,                      /*!< \brief UART rx characteristic */
  UART_RX_HDL,                         /*!< \brief UART rx value */
  UART_TX_CH_HDL,                      /*!< \brief UART tx characteristic */
  UART_TX_HDL,                         /*!< \brief UART tx value */
  UART_TX_CH_CCC_HDL,                  /*!< \brief UART tx CCCD */
  BATT_MAX_HDL                          /*!< \brief Maximum handle. */
};
/**@}*/

static const uint8_t UARTSvc[] = {0x9E,0xCA,0xDC,0x24,0x0E,0xE5,0xA9,0xE0,0x93,0xF3,0xA3,0xB5,0x01,0x00,0x40,0x6E};

static const uint8_t uartRxCh[] = {ATT_PROP_WRITE, UINT16_TO_BYTES(UART_RX_HDL), 0x9E,0xCA,0xDC,0x24,0x0E,0xE5,0xA9,0xE0,0x93,0xF3,0xA3,0xB5,0x02,0x00,0x40,0x6E};
const uint8_t attUartRxChUuid[] = {0x9E,0xCA,0xDC,0x24,0x0E,0xE5, 0xA9,0xE0,0x93,0xF3,0xA3,0xB5,0x02,0x00,0x40,0x6E};

static const uint8_t uartTxCh[] = {ATT_PROP_READ | ATT_PROP_NOTIFY, UINT16_TO_BYTES(UART_TX_HDL), 0x9E,0xCA,0xDC,0x24,0x0E,0xE5,0xA9,0xE0,0x93,0xF3,0xA3,0xB5,0x03,0x00,0x40,0x6E};
const uint8_t attUartTxChUuid[] = {0x9E,0xCA,0xDC,0x24,0x0E,0xE5, 0xA9,0xE0,0x93,0xF3,0xA3,0xB5,0x03,0x00,0x40,0x6E};
/* Battery level client characteristic configuration */



void *SvcUARTAddGroupDyn(void)
{
  void *pSHdl;
  uint8_t initCcc[] = {UINT16_TO_BYTES(0x0000)};
  uint8_t initUARTVal[] = {0x20};

  /* Create the service */
  pSHdl = AttsDynCreateGroup(UART_START_HDL, UART_END_HDL);

  if (pSHdl != NULL)
  {
    /* Primary service */
    AttsDynAddAttrConst(pSHdl, attPrimSvcUuid, UARTSvc, sizeof(UARTSvc), 0, ATTS_PERMIT_READ);


    /* UART rx characteristic */
    AttsDynAddAttrConst(pSHdl, attChUuid, uartRxCh, sizeof(uartRxCh), 0, ATTS_PERMIT_READ);
    // XXX: attUartRxChUuid is 16 bytes but nothing says so....
    /* UART rx value */
    // XXX: not sure if max value of 128 is fine...
    AttsDynAddAttr(pSHdl, attUartRxChUuid, NULL, 0, 128,
                   ATTS_SET_WRITE_CBACK | ATTS_SET_VARIABLE_LEN, ATTS_PERMIT_WRITE);


    /* UART tx characteristic */
    AttsDynAddAttrConst(pSHdl, attChUuid, uartTxCh, sizeof(uartTxCh), 0, ATTS_PERMIT_READ);
    /* UART tx value */
    /* TODO: do we need ATTS_SET_READ_CBACK ? */
    AttsDynAddAttr(pSHdl, attUartTxChUuid, initUARTVal, sizeof(uint8_t), sizeof(uint8_t),
                   0, ATTS_PERMIT_READ);
    /* UART tx CCC descriptor */
    AttsDynAddAttr(pSHdl, attCliChCfgUuid, initCcc, sizeof(uint16_t), sizeof(uint16_t),
                   ATTS_SET_CCC, ATTS_PERMIT_READ | ATTS_PERMIT_WRITE);
  }

  return pSHdl;
}

dmConnId_t active_connection = 0;

#if 0
uint8_t UARTReadCback(dmConnId_t connId, uint16_t handle, uint8_t operation,
                     uint16_t offset, attsAttr_t *pAttr)
{
  //AppHwBattRead(pAttr->pValue);

  return ATT_SUCCESS;
}
#endif

uint8_t UARTWriteCback(dmConnId_t connId, uint16_t handle, uint8_t operation,
                          uint16_t offset, uint16_t len, uint8_t *pValue,
                          attsAttr_t *pAttr)
{
  active_connection = connId;

  //printf("UARTWriteCback %d: ", len);
  int i;
  for(i=0; i<len; i++) {
    //printf("%c", pValue[i]);
    serial_enqueue_char(pValue[i]);
  }
  serial_enqueue_char('\r');
  //printf("\n");
 
#if 0
  AttsSetAttr(UART_TX_HDL, len, pValue);
  AttsHandleValueNtf(connId, UART_TX_HDL, len, pValue);
#endif

  return ATT_SUCCESS;
}

uint8_t ble_uart_tx_buf[129];
uint8_t ble_uart_buf_tx_fill;

void ble_uart_write(uint8_t *pValue, uint8_t len)
{
  int i;
  for(i=0; i<len; i++) {
    if(pValue[i] >= 0x20 && pValue[i] < 0x7f) {
        ble_uart_tx_buf[ble_uart_buf_tx_fill] = pValue[i];
        ble_uart_buf_tx_fill++;
    } else if(pValue[i] >= '\r') {
        if(ble_uart_buf_tx_fill > 0) {
            AttsSetAttr(UART_TX_HDL, ble_uart_buf_tx_fill, ble_uart_tx_buf);
            if(active_connection) {
                AttsHandleValueNtf(active_connection, UART_TX_HDL, ble_uart_buf_tx_fill, ble_uart_tx_buf);
            }
            ble_uart_buf_tx_fill = 0;
        }
    }
  }
}


299
300
301
static void ble_init(void)
{
    WsfInit();
302
303
304
305
306
    StackInitFit();
    NVIC_SetPriority(BTLE_SFD_TO_IRQn, 2);
    NVIC_SetPriority(BTLE_TX_DONE_IRQn, 2);
    NVIC_SetPriority(BTLE_RX_RCVD_IRQn, 2);
    FitStart();
307

308
309
310
311
312
313
314
315
    /* Add the UART service dynamically */
    AttsDynInit();
    void *pSHdl;
    pSHdl = SvcUARTAddGroupDyn();
    //AttsDynRegister(pSHdl, UARTReadCback, UARTWriteCback);
    AttsDynRegister(pSHdl, NULL, UARTWriteCback);


316
317
    /* Register a handler for Application events */
    AppUiActionRegister(SetAddress);
318
319

    lasttick = xTaskGetTickCount();
320

321
322
323
324
325
326
327
    timerWakeup = xTimerCreateStatic(
        "timerWakeup", /* name */
        pdMS_TO_TICKS(1), /* period/time */
        pdFALSE, /* auto reload */
        NULL, /* timer ID */
        vTimerCallback, &x); /* callback */
}
328
329
330

void vBleTask(void*pvParameters)
{
331
	ble_task_id = xTaskGetCurrentTaskHandle();
332
    ble_init();
333

334
    while (1){
335
		ulTaskNotifyTake(pdTRUE, portTICK_PERIOD_MS * 1000);
336
        wsfOsDispatcher();
337
        scheduleTimer();
338
339
    }
}