source: azure_iot_hub_f767zi/trunk/asp_baseplatform/gdic/ble_shield2.1/BLECharacteristic.c@ 457

Last change on this file since 457 was 457, 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: 7.9 KB
Line 
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/*
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 <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
72static unsigned int
73convert_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
85static int
86uuid_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 */
110void
111BLEService_setup(BLEService* pserv, const char *uuid)
112{
113 uuid_setup(&pserv->_uuid, uuid);
114 pserv->_type = BLETypeService;
115}
116
117/*
118 * BLECharacteristicセットアップ
119 */
120void
121BLECharacteristic_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 */
139bool_t
140BLECharacteristic_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 */
161bool_t
162BLECharacteristic_setValue2(BLECharacteristic *pcharc, const char* value)
163{
164 return BLECharacteristic_setValue1(pcharc, (const unsigned char *)value, strlen(value));
165}
166
167/*
168 * BLECharacteristic 値設定(設定後通知)
169 */
170void
171BLECharacteristic_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 */
184bool_t
185BLECharacteristic_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 */
198bool_t
199BLECharacteristic_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 */
213void
214BLECharacteristic_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 */
226bool_t
227BLECharacteristic_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 */
241bool_t
242BLECharacteristic_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 */
256void
257BLECharacteristic_setEventHandler(BLECharacteristic *pcharc, uint8_t event, void *eventHandler)
258{
259 if(event < 3){
260 pcharc->_eventHandlers[event] = eventHandler;
261 }
262}
263
Note: See TracBrowser for help on using the repository browser.