source: azure_iot_hub_riscv/trunk/asp_baseplatform/gdic/ble_shield2.1/BLEPeripheral.c@ 453

Last change on this file since 453 was 453, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 16.2 KB
Line 
1/*
2 * TOPPERS BASE PLATFORM MIDDLEWARE
3 *
4 * Copyright (C) 2017-2018 by TOPPERS PROJECT
5 * Educational Working Group.
6 *
7 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
8 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
9 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
10 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
11 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
12 * スコード中に含まれていること.
13 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
14 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
15 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
16 * の無保証規定を掲載すること.
17 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
18 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
19 * と.
20 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
21 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
22 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
23 * 報告すること.
24 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
25 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
26 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
27 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
28 * 免責すること.
29 *
30 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
31 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
32 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
33 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
34 * の責任を負わない.
35 *
36 * @(#) $Id$
37 */
38
39/*
40The MIT License (MIT)
41
42Copyright (c) 2014 Sandeep Mistry
43
44Permission is hereby granted, free of charge, to any person obtaining a copy
45of this software and associated documentation files (the "Software"), to deal
46in the Software without restriction, including without limitation the rights
47to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
48copies of the Software, and to permit persons to whom the Software is
49furnished to do so, subject to the following conditions:
50
51The above copyright notice and this permission notice shall be included in all
52copies or substantial portions of the Software.
53
54THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
56FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
57AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
59OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
60SOFTWARE.
61*/
62
63#include <utility/lib_aci.h>
64#include <utility/aci_setup.h>
65#include "BLECentral.h"
66#include "BLECharacteristic.h"
67#include "BLEPeripheral.h"
68
69
70#define DEFAULT_DEVICE_NAME "Arduino"
71#define DEFAULT_APPEARANCE 0x0000
72
73static unsigned char _devicevalue[NUM_DEVICE_NAME];
74static unsigned char _appearvalue[NUM_APPEAR_VALUE];
75static unsigned char _servicevalue[NUM_SERVICE_VALUE];
76
77static BLEService _genericAccessService = {
78 {"1800", {0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 2},
79 BLETypeService
80};
81
82static BLECharacteristic _deviceNameCharacteristic = {
83 {{"2a00", {0x00, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 2},
84 BLETypeCharacteristic},
85 BLERead, /* _properties */
86 19, /* _valueSize */
87 _devicevalue, /* _value */
88 0, /* _valueLength */
89 0, /* _fixedLength */
90 false, /* _notify */
91 false, /* _written */
92 false, /* _subscribed */
93 NULL, /* _pblePeripheral */
94 NULL, /* characteristicValueChanged */
95 NULL, /* canNotifyCharacteristic */
96 NULL, /* canIndicateCharacteristic */
97 {0, 0, 0} /* _eventHandlers[3] */
98};
99
100static BLECharacteristic _appearanceCharacteristic = {
101 {{"2a01", {0x01, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 2},
102 BLETypeCharacteristic},
103 BLERead, /* _properties */
104 2, /* _valueSize */
105 _appearvalue, /* _value */
106 0, /* _valueLength */
107 0, /* _fixedLength */
108 false, /* _notify */
109 false, /* _written */
110 false, /* _subscribed */
111 NULL, /* _pblePeripheral */
112 NULL, /* characteristicValueChanged */
113 NULL, /* canNotifyCharacteristic */
114 NULL, /* canIndicateCharacteristic */
115 {0, 0, 0} /* _eventHandlers[3] */
116};
117
118static BLEService _genericAttributeService = {
119 {"1801", {0x01, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 2},
120 BLETypeService
121};
122
123static BLECharacteristic _servicesChangedCharacteristic = {
124 {{"2a05", {0x05, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 2},
125 BLETypeCharacteristic},
126 BLEIndicate, /* _properties */
127 4, /* _valueSize */
128 _servicevalue, /* _value */
129 0, /* _valueLength */
130 0, /* _fixedLength */
131 false, /* _notify */
132 false, /* _written */
133 false, /* _subscribed */
134 NULL, /* _pblePeripheral */
135 NULL, /* characteristicValueChanged */
136 NULL, /* canNotifyCharacteristic */
137 NULL, /* canIndicateCharacteristic */
138 {0, 0, 0} /* _eventHandlers[3] */
139};
140
141static bool_t
142BLEPeripheral_characteristicValueChanged(nRF8001* nRF8001, BLECharacteristic *pcharc)
143{
144 return nRF8001_updateCharacteristicValue(nRF8001, pcharc);
145}
146
147static bool_t
148BLEPeripheral_canNotifyCharacteristic(nRF8001* nRF8001, BLECharacteristic *pcharc)
149{
150 return nRF8001_canNotifyCharacteristic(nRF8001, pcharc);
151}
152
153static bool_t
154BLEPeripheral_canIndicateCharacteristic(nRF8001* nRF8001, BLECharacteristic *pcharc)
155{
156 return nRF8001_canIndicateCharacteristic(nRF8001, pcharc);
157}
158
159
160/*
161 * BLEPeripheralのセットアップ
162 */
163void
164BLEPeripheral_setup(BLEPeripheral *pblep, unsigned char req, unsigned char rdy, unsigned char rst)
165{
166 memset(pblep, 0, sizeof(BLEPeripheral));
167 pblep->_nRF8001._aciState.aci_pins.reqn_pin = req;
168 pblep->_nRF8001._aciState.aci_pins.rdyn_pin = rdy;
169 pblep->_nRF8001._aciState.aci_pins.reset_pin = rst;
170
171 pblep->_nRF8001._aciState.aci_pins.board_name = BOARD_DEFAULT;
172 pblep->_nRF8001._aciState.aci_pins.active_pin = UNUSED;
173 pblep->_nRF8001._aciState.aci_pins.optional_chip_sel_pin = UNUSED;
174 pblep->_nRF8001._aciState.aci_pins.interface_is_interrupt = false;
175 pblep->_nRF8001._aciState.aci_pins.event_taskid = 0;
176 pblep->_nRF8001._pipeInfo = NULL;
177 pblep->_nRF8001._numPipeInfo = 0;
178 pblep->_nRF8001._crcSeed = 0xFFFF;
179 pblep->_nRF8001._pblePeripheral = pblep;
180
181 pblep->_attributes[0] = &_genericAccessService;
182 _deviceNameCharacteristic._valueLength = strlen(DEFAULT_DEVICE_NAME);
183 memcpy(_devicevalue, DEFAULT_DEVICE_NAME, _deviceNameCharacteristic._valueLength);
184 _deviceNameCharacteristic._pblePeripheral = pblep;
185 pblep->_attributes[1] = (BLEAttribute*)&_deviceNameCharacteristic;
186 _appearanceCharacteristic._pblePeripheral = pblep;
187 pblep->_attributes[2] = (BLEAttribute*)&_appearanceCharacteristic;
188 pblep->_attributes[3] = &_genericAttributeService;
189 _servicesChangedCharacteristic._pblePeripheral = pblep;
190 pblep->_attributes[4] = (BLEAttribute*)&_servicesChangedCharacteristic;
191 pblep->_numAttributes = 5;
192 pblep->_central._peripheral = pblep;
193 pblep->_central._connected = false;
194 memset(pblep->_central._address, 0x00, 6);
195
196 pblep->_manufacturerData = NULL;
197 pblep->_manufacturerDataLength = 0;
198}
199
200/*
201 * BLEPeripheral開始
202 */
203void
204BLEPeripheral_begin(BLEPeripheral *pblep)
205{
206 uint8_t advertisementData[20];
207 uint8_t scanData[20];
208 uint8_t advertisementDataLength = 0;
209 uint8_t scanDataLength = 0;
210 int i;
211
212 if(pblep->_advertisedServiceUuid){
213 unsigned char advertisedServiceUuidLength = sizeof(pblep->_advertisedServiceUuid);
214 advertisementDataLength = 2 + advertisedServiceUuidLength;
215 advertisementData[0] = (advertisedServiceUuidLength > 2) ? 0x06 : 0x02;
216 advertisementData[1] = advertisedServiceUuidLength;
217 memcpy(&advertisementData[2], pblep->_advertisedServiceUuid, advertisedServiceUuidLength);
218 }
219 else if(pblep->_manufacturerData && pblep->_manufacturerDataLength > 0) {
220 if(pblep->_manufacturerDataLength > sizeof(advertisementData))
221 pblep->_manufacturerDataLength = sizeof(advertisementData);
222
223 advertisementDataLength = 2 + pblep->_manufacturerDataLength;
224 advertisementData[0] = 0xff;
225 advertisementData[1] = pblep->_manufacturerDataLength;
226 memcpy(&advertisementData[2], pblep->_manufacturerData, pblep->_manufacturerDataLength);
227 }
228
229 if(pblep->_localName){
230 unsigned char originalLocalNameLength = strlen(pblep->_localName);
231 unsigned char localNameLength = originalLocalNameLength;
232
233 if(localNameLength > sizeof(scanData))
234 localNameLength = sizeof(scanData);
235 scanDataLength = 2 + localNameLength;
236 scanData[0] = (originalLocalNameLength > sizeof(scanData)) ? 0x08 : 0x09;
237 scanData[1] = localNameLength;
238 memcpy(&scanData[2], pblep->_localName, localNameLength);
239 }
240
241 for(i = 0 ; i < pblep->_numAttributes ; i++){
242 BLEAttribute* attribute = pblep->_attributes[i];
243 if(attribute->_type == BLETypeCharacteristic){
244 BLECharacteristic* pcharc = (BLECharacteristic*)attribute;
245 pcharc->characteristicValueChanged = BLEPeripheral_characteristicValueChanged;
246 pcharc->canNotifyCharacteristic = BLEPeripheral_canNotifyCharacteristic;
247 pcharc->canIndicateCharacteristic = BLEPeripheral_canIndicateCharacteristic;
248 }
249 }
250
251 nRF8001_begin(&pblep->_nRF8001, advertisementData, advertisementDataLength, scanData, scanDataLength, pblep->_attributes, pblep->_numAttributes);
252 nRF8001_requestAddress();
253}
254
255/*
256 * BLEPeripheralの状態ポーリング
257 */
258void
259BLEPeripheral_poll(BLEPeripheral *pblep)
260{
261 nRF8001_poll(&pblep->_nRF8001);
262}
263
264/*
265 * BLEPeripheral UUIDのセット
266 */
267void
268BLEPeripheral_setAdvertisedServiceUuid(BLEPeripheral *pblep, const char* advertisedServiceUuid)
269{
270 pblep->_advertisedServiceUuid = advertisedServiceUuid;
271}
272
273/*
274 * BLEPeripheral MANUFACURER DATAのセット
275 */
276void
277BLEPeripheral_setManufacturerData(BLEPeripheral *pblep, const unsigned char manufacturerData[], unsigned char manufacturerDataLength)
278{
279 pblep->_manufacturerData = manufacturerData;
280 pblep->_manufacturerDataLength = manufacturerDataLength;
281}
282
283/*
284 * BLEPeripheral LOCAL NAMEのセット
285 */
286void
287BLEPeripheral_setLocalName(BLEPeripheral *pblep, const char* localName)
288{
289 pblep->_localName = localName;
290}
291
292/*
293 * BLEPeripheral ATTRIBUTEの追加
294 */
295void
296BLEPeripheral_addAttribute(BLEPeripheral *pblep, BLEAttribute* attribute)
297{
298 if(attribute->_type == BLETypeCharacteristic){
299 BLECharacteristic *pcharc = (BLECharacteristic*)attribute;
300 pcharc->_pblePeripheral = pblep;
301 }
302 pblep->_attributes[pblep->_numAttributes++] = attribute;
303}
304
305/*
306 * BLEPeripheral DEVICENMAEのセット
307 */
308void
309BLEPeripheral_setDeviceName(BLEPeripheral *pblep, const char* deviceName)
310{
311 BLECharacteristic_setValue2(&_deviceNameCharacteristic, deviceName);
312}
313
314/*
315 * BLEPeripheral APPEARANCEのセット
316 */
317void
318BLEPeripheral_setAppearance(BLEPeripheral *pblep, unsigned short appearance)
319{
320 BLECharacteristic_setValue1(&_appearanceCharacteristic, (unsigned char *)&appearance, sizeof(appearance));
321}
322
323/*
324 * BLEPeripheral切断
325 */
326void
327BLEPeripheral_disconnect(BLEPeripheral *pblep)
328{
329 nRF8001_disconnect(&pblep->_nRF8001);
330}
331
332/*
333 * BLEPeripheral セントラル構造体の取り出し
334 */
335BLECentral *
336BLEPeripheral_central(BLEPeripheral *pblep)
337{
338 BLEPeripheral_poll(pblep);
339 return &pblep->_central;
340}
341
342/*
343 * BLEPeripheral セントラル接続判定
344 */
345bool_t
346BLEPeripheral_connected(BLEPeripheral *pblep)
347{
348 BLEPeripheral_poll(pblep);
349 return BLECentral_connected(&pblep->_central);
350}
351
352/*
353 * BLEPeripheral ペリフェラルのイベントハンドラを設定
354 */
355void
356BLEPeripheral_setEventHandler(BLEPeripheral *pblep, uint8_t event, BLEPeripheralEventHandler eventHandler)
357{
358 if(event < 2){
359 pblep->_eventHandlers[event] = eventHandler;
360 }
361}
362
363/*
364 * BLEPeripheral バージョン取得
365 */
366bool_t
367BLEPeripheral_version(BLEPeripheral *pblep)
368{
369 return lib_aci_device_version();
370}
371
372
373/*
374 * nRF8001Connectedデフォルトコールバック関数
375 */
376void
377BLEPeripheral_nRF8001Connected(nRF8001* nRF8001, const unsigned char* address)
378{
379 BLEPeripheral *pblep = nRF8001->_pblePeripheral;
380 memcpy(pblep->_central._address, address, 6);
381 pblep->_central._connected = true;
382
383#ifdef BLE_PERIPHERAL_DEBUG
384 syslog_5(LOG_NOTICE, "Peripheral connected to central:%02:%02:%02:%02:%02:XX ", address[0], address[1], address[2], address[3], address[4]);
385#endif
386
387 BLEPeripheralEventHandler eventHandler = pblep->_eventHandlers[BLEConnected];
388 if(eventHandler)
389 eventHandler(&pblep->_central);
390}
391
392/*
393 * nRF8001Disconnectedデフォルトコールバック関数
394 */
395void
396BLEPeripheral_nRF8001Disconnected(nRF8001* nRF8001)
397{
398 BLEPeripheral *pblep = nRF8001->_pblePeripheral;
399#ifdef BLE_PERIPHERAL_DEBUG
400 unsigned char *address = pblep->_central._address;
401 syslog_5(LOG_NOTICE, "Peripheral disconnected from central:%02:%02:%02:%02:%02:XX ", address[0], address[1], address[2], address[3], address[4]);
402#endif
403
404 BLEPeripheralEventHandler eventHandler = pblep->_eventHandlers[BLEDisconnected];
405 if(eventHandler)
406 eventHandler(&pblep->_central);
407 pblep->_central._connected = false;
408}
409
410/*
411 * nRF8001CharacteristicValueChangedデフォルトコールバック関数
412 */
413void
414BLEPeripheral_nRF8001CharacteristicValueChanged(nRF8001* nRF8001, BLECharacteristic *pcharc, const unsigned char* value, unsigned char valueLength)
415{
416 BLEPeripheral *pblep = nRF8001->_pblePeripheral;
417
418 if(valueLength > pcharc->_valueSize)
419 pcharc->_valueLength = pcharc->_valueSize;
420 else
421 pcharc->_valueLength = valueLength;
422 memcpy(pcharc->_value, value, pcharc->_valueLength);
423
424 pcharc->_notify = true;
425 pcharc->_written = true;
426
427 BLECharacteristicEventHandler eventHandler = pcharc->_eventHandlers[BLEWritten];
428 if(eventHandler)
429 eventHandler(&pblep->_central, pcharc);
430}
431
432/*
433 * nRF8001CharacteristicSubscribedChangedデフォルトコールバック関数
434 */
435void
436BLEPeripheral_nRF8001CharacteristicSubscribedChanged(nRF8001* nRF8001, BLECharacteristic *pcharc, bool_t subscribed)
437{
438 BLEPeripheral *pblep = nRF8001->_pblePeripheral;
439 pcharc->_subscribed = subscribed;
440
441 BLECharacteristicEventHandler eventHandler = pcharc->_eventHandlers[subscribed ? BLESubscribed : BLEUnsubscribed];
442
443 if(eventHandler)
444 eventHandler(&pblep->_central, pcharc);
445}
446
447/*
448 * nRF8001AddressReceivedデフォルトコールバック関数
449 */
450void
451BLEPeripheral_nRF8001AddressReceived(nRF8001* nRF8001, const unsigned char* address)
452{
453#ifdef BLE_PERIPHERAL_DEBUG
454 syslog_5(LOG_NOTICE, "Peripheral address:%02:%02:%02:%02:%02:XX ", address[0], address[1], address[2], address[3], address[4]);
455#endif
456}
457
458/*
459 * nRF8001TemperatureReceivedデフォルトコールバック関数
460 */
461void
462BLEPeripheral_nRF8001TemperatureReceived(nRF8001* nRF8001, float temperature)
463{
464#ifdef BLE_PERIPHERAL_DEBUG
465 int32_t tmp1 = temperature;
466 uint32_t tmp2;
467
468 tmp2 = (temperature - tmp1) * 100;
469 syslog_2(LOG_NOTICE, "Temperature = %d.%02d", tmp1, tmp2);
470#endif
471}
472
473/*
474 * nRF8001BatteryLevelReceivedデフォルトコールバック関数
475 */
476void
477BLEPeripheral_nRF8001BatteryLevelReceived(nRF8001* nRF8001, float batteryLevel)
478{
479#ifdef BLE_PERIPHERAL_DEBUG
480 int32_t tmp1 = batteryLevel;
481 uint32_t tmp2;
482
483 tmp2 = (batteryLevel - tmp1) * 10000;
484 syslog_2(LOG_NOTICE, "Battery level = %d.%04d", tmp1, tmp2);
485#endif
486}
487
Note: See TracBrowser for help on using the repository browser.