Newer
Older
#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"
#include "hci_vs.h"
#include "ff.h"
#include "util/bstream.h"
#include "att_api.h"
#include "FreeRTOS.h"
#include "crc32.h"
#include "epicardium.h"
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <machine/endian.h>
#define CARD10_UUID_SUFFIX \
0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23
/*!< \brief Service start handle. */
#define CARD10_START_HDL 0x920
/*!< \brief Service end handle. */
#define CARD10_END_HDL (CARD10_MAX_HDL - 1)
/*
* This has to match in order and number of members to the functions
* called in addCard10GroupDyn() otherwise the stack breaks.
*/
enum {
/*!< \brief card10 service declaration */
CARD10_SVC_HDL = CARD10_START_HDL,
/*!< \brief vibra characteristic */
CARD10_VIRBA_CH_HDL,
CARD10_VIBRA_VAL_HDL,
/*!< \brief rockets led characteristic */
CARD10_ROCKETS_CH_HDL,
CARD10_ROCKETS_VAL_HDL,
/*!< \brief light sensor characteristic */
CARD10_LIGHT_SENSOR_CH_HDL,
CARD10_LIGHT_SENSOR_VAL_HDL,
/*!< \brief time update characteristic */
CARD10_TIME_UPDATE_CH_HDL,
CARD10_TIME_UPDATE_VAL_HDL,
/*!< \brief Maximum handle. */
CARD10_MAX_HDL
};
/* BLE UUID for card10 service*/
static const uint8_t UUID_svc[] = { CARD10_UUID_SUFFIX, 0x0, CARD10_UUID_PREFIX };
/* BLE UUID for card10 char vibra */
static const uint8_t UUID_char_vibra[] = {
ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_VIBRA_VAL_HDL),
static const uint8_t UUID_attChar_vibra[] = {
};
/* BLE UUID for card10 char rockets */
static const uint8_t UUID_char_rockets[] = {
ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_ROCKETS_VAL_HDL),
static const uint8_t UUID_attChar_rockets[] = {
};
/* BLE UUID for card10 char light sensor */
static const uint8_t UUID_char_light_sensor[] = {
ATT_PROP_READ,
UINT16_TO_BYTES(CARD10_LIGHT_SENSOR_VAL_HDL),
};
static const uint8_t UUID_attChar_light_sensor[] = {
/* BLE UUID for card10 time update */
static const uint8_t UUID_char_time[] = {
ATT_PROP_WRITE,
UINT16_TO_BYTES(CARD10_TIME_UPDATE_VAL_HDL),
CARD10_UUID_SUFFIX, 0x01, CARD10_UUID_PREFIX
};
static const uint8_t UUID_attChar_time[] = {
CARD10_UUID_SUFFIX, 0x01, CARD10_UUID_PREFIX
};
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
/*
* Create the BLE service description.
*/
static void *addCard10GroupDyn(void)
{
void *pSHdl;
uint8_t initLightSensorValue[] = { UINT16_TO_BYTES(0) };
/* Create the service */
pSHdl = AttsDynCreateGroup(CARD10_START_HDL, CARD10_END_HDL);
if (pSHdl != NULL) {
/* Primary service */
AttsDynAddAttrConst(
pSHdl,
attPrimSvcUuid,
UUID_svc,
sizeof(UUID_svc),
0,
ATTS_PERMIT_READ
);
// VIBRA
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_vibra,
sizeof(UUID_char_vibra),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_vibra,
NULL,
sizeof(uint16_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE
);
// ROCKETS
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_rockets,
sizeof(UUID_char_rockets),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_rockets,
NULL,
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
3 * sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE
);
// LIGHT_SENSOR
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_light_sensor,
sizeof(UUID_char_light_sensor),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_light_sensor,
initLightSensorValue,
sizeof(uint8_t),
sizeof(uint8_t),
ATTS_SET_READ_CBACK,
ATTS_PERMIT_READ
);
// TIME UPDTAE
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_time,
sizeof(UUID_char_time),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_time,
NULL,
0,
sizeof(uint64_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE
);
}
return pSHdl;
}
/*
* Set the time given in milliseconds since 1.1.1970 as 64 bit integer.
*/
static uint8_t setTime(uint8_t *pValue, uint16_t len)
{
uint64_t timeNet;
uint64_t time;
if (len < sizeof(uint64_t)) {
return ATT_ERR_LENGTH;
}
memcpy(&timeNet, pValue, sizeof(timeNet));
time = __bswap64(timeNet);
epic_rtc_set_milliseconds(time);
return ATT_SUCCESS;
}
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
*/
static uint8_t writeCard10CB(
dmConnId_t connId,
uint16_t handle,
uint8_t operation,
uint16_t offset,
uint16_t len,
uint8_t *pValue,
attsAttr_t *pAttr
) {
uint16_t ui16;
switch (handle) {
case CARD10_VIBRA_VAL_HDL:
BYTES_TO_UINT16(ui16, pValue);
epic_vibra_vibrate(ui16);
APP_TRACE_INFO1("ble-card10: set vibra for %dms\n", ui16);
return ATT_SUCCESS;
case CARD10_ROCKETS_VAL_HDL:
epic_leds_set_rocket(0, pValue[0]);
epic_leds_set_rocket(1, pValue[1]);
epic_leds_set_rocket(2, pValue[2]);
APP_TRACE_INFO3(
"ble-card10: set rockets 0:%d, 1:%d, 2:%d\n",
pValue[0],
pValue[1],
pValue[2]
);
return ATT_SUCCESS;
case CARD10_TIME_UPDATE_VAL_HDL:
if (operation == ATT_PDU_PREP_WRITE_REQ) {
if (len < sizeof(uint64_t)) {
return ATT_ERR_LENGTH;
}
return ATT_SUCCESS;
}
return setTime(pValue, len);
default:
APP_TRACE_INFO1(
"ble-card10: unsupported characteristic: %c\n", handle
);
return ATT_ERR_HANDLE;
}
}
static uint8_t readCard10CB(
dmConnId_t connId,
uint16_t handle,
uint8_t operation,
uint16_t offset,
attsAttr_t *pAttr
) {
uint16_t ui16 = 0;
switch (handle) {
case CARD10_LIGHT_SENSOR_VAL_HDL:
epic_light_sensor_get(&ui16);
*pAttr->pValue = ui16;
APP_TRACE_INFO1("ble-card10: read lightsensor: %d\n", ui16);
return ATT_SUCCESS;
default:
APP_TRACE_INFO0("ble-card10: read no handler found\n");
return ATT_ERR_HANDLE;
}
}
/*