1 | /*
|
---|
2 | * TOPPERS BASE PLATFORM MIDDLEWARE
|
---|
3 | *
|
---|
4 | * Copyright (C) 2017-2019 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 | /*
|
---|
40 | The MIT License (MIT)
|
---|
41 |
|
---|
42 | Copyright (c) 2014 Sandeep Mistry
|
---|
43 |
|
---|
44 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
45 | of this software and associated documentation files (the "Software"), to deal
|
---|
46 | in the Software without restriction, including without limitation the rights
|
---|
47 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
48 | copies of the Software, and to permit persons to whom the Software is
|
---|
49 | furnished to do so, subject to the following conditions:
|
---|
50 |
|
---|
51 | The above copyright notice and this permission notice shall be included in all
|
---|
52 | copies or substantial portions of the Software.
|
---|
53 |
|
---|
54 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
55 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
56 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
57 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
58 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
59 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
---|
60 | SOFTWARE.
|
---|
61 | */
|
---|
62 |
|
---|
63 | #include <stdlib.h>
|
---|
64 | #include <utility/lib_aci.h>
|
---|
65 | #include <utility/aci_setup.h>
|
---|
66 | #include "BLECentral.h"
|
---|
67 | #include "BLECharacteristic.h"
|
---|
68 | #include "BLEPeripheral.h"
|
---|
69 |
|
---|
70 | #define MIN(a, b) ((a) > (b) ? (b) : (a))
|
---|
71 |
|
---|
72 | static unsigned int
|
---|
73 | convert_hex(const char a)
|
---|
74 | {
|
---|
75 | if(a >= '0' && a <= '9')
|
---|
76 | return a - '0';
|
---|
77 | else if(a >= 'A' && a <= 'F')
|
---|
78 | return a - 'A' + 10;
|
---|
79 | else if(a >= 'a' && a <= 'f')
|
---|
80 | return a - 'a' + 10;
|
---|
81 | else
|
---|
82 | return 0;
|
---|
83 | }
|
---|
84 |
|
---|
85 | static int
|
---|
86 | uuid_setup(BLEUuid* puuid, const char *str)
|
---|
87 | {
|
---|
88 | int i, len = 0;
|
---|
89 |
|
---|
90 | puuid->_str = str;
|
---|
91 | puuid->_length = 0;
|
---|
92 | for (i = strlen(str) - 1; i >= 0 && puuid->_length < MAX_UUID_LENGTH; i -= 2) {
|
---|
93 | if (str[i] == '-') {
|
---|
94 | i++;
|
---|
95 | continue;
|
---|
96 | }
|
---|
97 |
|
---|
98 | len = convert_hex(str[i - 1]) << 4;
|
---|
99 | len |= convert_hex(str[i]);
|
---|
100 | puuid->_data[puuid->_length] = len;
|
---|
101 |
|
---|
102 | puuid->_length++;
|
---|
103 | }
|
---|
104 | return puuid->_length;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * BLEServiceセットアップ
|
---|
109 | */
|
---|
110 | void
|
---|
111 | BLEService_setup(BLEService* pserv, const char *uuid)
|
---|
112 | {
|
---|
113 | uuid_setup(&pserv->_uuid, uuid);
|
---|
114 | pserv->_type = BLETypeService;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * BLECharacteristicセットアップ
|
---|
119 | */
|
---|
120 | void
|
---|
121 | BLECharacteristic_setup(BLECharacteristic* pcharc, const char *uuid, unsigned char properties, unsigned char *value, unsigned char len)
|
---|
122 | {
|
---|
123 | memset(pcharc, 0, sizeof(BLECharacteristic));
|
---|
124 | uuid_setup(&pcharc->_attr._uuid, uuid);
|
---|
125 | pcharc->_attr._type = BLETypeCharacteristic;
|
---|
126 | pcharc->_properties = properties;
|
---|
127 | pcharc->_valueSize = MIN(BLE_ATTRIBUTE_MAX_VALUE_LENGTH, len);
|
---|
128 | pcharc->_value = value;
|
---|
129 | pcharc->_valueLength = 0;
|
---|
130 | pcharc->_fixedLength = 0;
|
---|
131 | pcharc->_notify = false;
|
---|
132 | pcharc->_written = false;
|
---|
133 | pcharc->_subscribed = false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * BLECharacteristic値設定(値の長さあり)
|
---|
138 | */
|
---|
139 | bool_t
|
---|
140 | BLECharacteristic_setValue1(BLECharacteristic *pcharc, const unsigned char value[], unsigned char length)
|
---|
141 | {
|
---|
142 | bool_t success = true;
|
---|
143 |
|
---|
144 | if(length > pcharc->_valueSize)
|
---|
145 | pcharc->_valueLength = pcharc->_valueSize;
|
---|
146 | else
|
---|
147 | pcharc->_valueLength = length;
|
---|
148 |
|
---|
149 | memcpy(pcharc->_value, value, pcharc->_valueLength);
|
---|
150 |
|
---|
151 | if(pcharc->characteristicValueChanged != NULL){
|
---|
152 | BLEPeripheral *pblep = pcharc->_pblePeripheral;
|
---|
153 | success = pcharc->characteristicValueChanged(&pblep->_nRF8001, pcharc);
|
---|
154 | }
|
---|
155 | return success;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * BLECharacteristic 値設定(値の長さなし)
|
---|
160 | */
|
---|
161 | bool_t
|
---|
162 | BLECharacteristic_setValue2(BLECharacteristic *pcharc, const char* value)
|
---|
163 | {
|
---|
164 | return BLECharacteristic_setValue1(pcharc, (const unsigned char *)value, strlen(value));
|
---|
165 | }
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * BLECharacteristic 値設定(設定後通知)
|
---|
169 | */
|
---|
170 | void
|
---|
171 | BLECharacteristic_setValue(BLECharacteristic *pcharc, BLECentral* central, const unsigned char value[], unsigned char length)
|
---|
172 | {
|
---|
173 | BLECharacteristicEventHandler eventHandler = pcharc->_eventHandlers[BLEWritten];
|
---|
174 |
|
---|
175 | BLECharacteristic_setValue1(pcharc, value, length);
|
---|
176 | pcharc->_written = true;
|
---|
177 | if(eventHandler != NULL)
|
---|
178 | eventHandler(central, pcharc);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * BLECharacteristic 通知確認
|
---|
183 | */
|
---|
184 | bool_t
|
---|
185 | BLECharacteristic_notified(BLECharacteristic *pcharc)
|
---|
186 | {
|
---|
187 | bool_t notify = pcharc->_notify;
|
---|
188 |
|
---|
189 | if(notify){
|
---|
190 | pcharc->_notify = false;
|
---|
191 | }
|
---|
192 | return notify;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * BLECharacteristic 書き込み確認
|
---|
197 | */
|
---|
198 | bool_t
|
---|
199 | BLECharacteristic_written(BLECharacteristic *pcharc)
|
---|
200 | {
|
---|
201 | bool_t written = pcharc->_written;
|
---|
202 |
|
---|
203 | if(written){
|
---|
204 | pcharc->_written = false;
|
---|
205 | }
|
---|
206 | return written;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * BLECharacteristic Subscribed設定
|
---|
212 | */
|
---|
213 | void
|
---|
214 | BLECharacteristic_setSubscribed(BLECharacteristic *pcharc, BLECentral* central, bool_t subscribed)
|
---|
215 | {
|
---|
216 | BLECharacteristicEventHandler eventHandler = pcharc->_eventHandlers[subscribed ? BLESubscribed : BLEUnsubscribed];
|
---|
217 |
|
---|
218 | pcharc->_subscribed = subscribed;
|
---|
219 | if(eventHandler != NULL)
|
---|
220 | eventHandler(central, pcharc);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /*
|
---|
224 | * BLECharacteristic Notify可能判定
|
---|
225 | */
|
---|
226 | bool_t
|
---|
227 | BLECharacteristic_canNotify(BLECharacteristic *pcharc)
|
---|
228 | {
|
---|
229 | BLEPeripheral *pblep = pcharc->_pblePeripheral;
|
---|
230 | if(pblep == NULL)
|
---|
231 | return false;
|
---|
232 | if(pcharc->canNotifyCharacteristic != NULL)
|
---|
233 | return pcharc->canNotifyCharacteristic(&pblep->_nRF8001, pcharc);
|
---|
234 | else
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /*
|
---|
239 | * BLECharacteristic Indicate可能判定
|
---|
240 | */
|
---|
241 | bool_t
|
---|
242 | BLECharacteristic_canIndicate(BLECharacteristic *pcharc)
|
---|
243 | {
|
---|
244 | BLEPeripheral *pblep = pcharc->_pblePeripheral;
|
---|
245 | if(pblep == NULL)
|
---|
246 | return false;
|
---|
247 | if(pcharc->canIndicateCharacteristic != NULL)
|
---|
248 | return pcharc->canIndicateCharacteristic(&pblep->_nRF8001, pcharc);
|
---|
249 | else
|
---|
250 | return false;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * BLECharacteristic コールバック関数設定
|
---|
255 | */
|
---|
256 | void
|
---|
257 | BLECharacteristic_setEventHandler(BLECharacteristic *pcharc, uint8_t event, void *eventHandler)
|
---|
258 | {
|
---|
259 | if(event < 3){
|
---|
260 | pcharc->_eventHandlers[event] = eventHandler;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|