Merge test folder of test branch
[iotivity.git] / test / src / common / commonutil / c_cpp / AM / dependencies / MySignals / bloodpressure.cpp
1 /******************************************************************
2 *
3 * Copyright 2018 Open Connectivity Foundation All Rights Reserved.
4 *
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************/
20
21 #include "dependencies/common.h"
22 #if USE_HW
23     #include "common.h"
24     #include "MySignals.h"
25     #include "MySignals_BLE.h"
26     #define USE_MYSIGNALS 1
27 #else
28     #include <string.h>
29     #include <inttypes.h>
30     #define USE_MYSIGNALS 0
31 #endif
32 #include "MySignals/bloodpressure.h"
33
34
35 //!Struct to store data of the BloodPressure
36 struct bloodPressureBLEDataVector
37 {
38   uint16_t systolic;
39   uint16_t diastolic;
40   uint16_t pulse;
41 };
42
43 //!Vector to store the glucometer measures and dates.
44 bloodPressureBLEDataVector bloodPressureBLEData;
45
46 // Write here the MAC address of BLE device to find
47 char MAC_BP[14] = "508CB1664FA6";
48
49 uint8_t available_bp = 0;
50 uint8_t connected_bp = 0;
51 uint8_t connection_handle_bp = 0;
52
53 #define BP_HANDLE 18
54
55 uint16_t getSystolic() {
56     return bloodPressureBLEData.systolic;
57 }
58
59 uint16_t getDiastolic() {
60     return bloodPressureBLEData.diastolic;
61 }
62
63 uint16_t getPulseRate() {
64     return bloodPressureBLEData.pulse;
65 }
66
67
68 uint8_t getMySignalBloodPressure() {
69     uint8_t ret = 0;
70     #if USE_HW
71     char strbuf[30] = {0x00, };
72     available_bp = MySignals_BLE.scanDevice(MAC_BP, 1000, TX_POWER_MAX);
73
74     MySignals.disableMuxUART();
75
76     memset(strbuf, 0x00, 30);
77     sprintf(strbuf, "BP available: %d\n", available_bp);
78     printf(strbuf);
79
80     MySignals.enableMuxUART();
81
82     printf ("start access \n");
83     if (available_bp == 1)
84     {
85
86         if (MySignals_BLE.connectDirect(MAC_BP) == 1)
87         {
88             MySignals.println("Connected");
89
90             connected_bp = 1;
91             delay(8000);
92             uint8_t data = 'e';
93             //To subscribe the BP measure write an ASCII letter "e"
94             if (MySignals_BLE.attributeWrite(MySignals_BLE.connection_handle,  BP_HANDLE, &data, 1) == 0)
95             {
96
97                 MySignals.println("Subscribed");
98
99                 unsigned long previous = millis();
100                 do
101                 {
102
103                     if (MySignals_BLE.waitEvent(1000) == BLE_EVENT_ATTCLIENT_ATTRIBUTE_VALUE)
104                     {
105                         //Search letter "g" in ASCII
106                         if (MySignals_BLE.event[9] == 0x67)
107                         {
108
109                             if (MySignals_BLE.event[10] == 0x2f)
110                             {
111                             //Ojo esto esta mal en la guia de comandos
112
113                             //Primero da la alta -> sistolica
114                             uint8_t sh = MySignals_BLE.event[11] - 48;
115                             uint8_t sm = MySignals_BLE.event[12] - 48;
116                             uint8_t sl = MySignals_BLE.event[13] - 48;
117                             bloodPressureBLEData.systolic = (sh * 100) + (sm * 10) + sl;
118
119                             //Primero da la baja -> diastolica
120                             uint8_t dh = MySignals_BLE.event[15] - 48;
121                             uint8_t dm = MySignals_BLE.event[16] - 48;
122                             uint8_t dl = MySignals_BLE.event[17] - 48;
123                             bloodPressureBLEData.diastolic = (dh * 100) + (dm * 10) + dl;
124
125                             uint8_t ph = MySignals_BLE.event[19] - 48;
126                             uint8_t pm = MySignals_BLE.event[20] - 48;
127                             uint8_t pl = MySignals_BLE.event[21] - 48;
128                             bloodPressureBLEData.pulse = (ph * 100) + (pm * 10) + pl;
129
130
131                             MySignals.disableMuxUART();
132                             memset(strbuf, 0x00, 30);
133                             sprintf(strbuf, "Systolic: %d", bloodPressureBLEData.systolic);
134                             MySignals.println(&(strbuf[0]));
135
136                             memset(strbuf, 0x00, 30);
137                             sprintf(strbuf, "Diastolic: %d", bloodPressureBLEData.diastolic);
138                             MySignals.println(&(strbuf[0]));
139
140                             memset(strbuf, 0x00, 30);
141                             sprintf(strbuf, "Pulse/min: %d", bloodPressureBLEData.pulse);
142                             MySignals.println(&(strbuf[0]));
143
144                             MySignals.enableMuxUART();
145
146                             connected_bp = 0;
147                             ret = 1;
148                             }
149                         }
150                     }
151                 }
152                 while ((connected_bp == 1) && ((millis() - previous) < 40000));
153
154                 data = 'i';
155                 MySignals_BLE.attributeWrite(MySignals_BLE.connection_handle,  BP_HANDLE, &data , 1);
156                 delay(100);
157
158
159                 MySignals_BLE.disconnect(MySignals_BLE.connection_handle);
160
161                 connected_bp = 0;
162
163             }
164             else
165             {
166                 MySignals.println("Error subscribing");
167             }
168
169         }
170         else
171         {
172             connected_bp = 0;
173             MySignals.println("Not Connected");
174         }
175     }
176     else if (available_bp == 0)
177     {
178         //Do nothing
179     }
180     else
181     {
182         MySignals_BLE.hardwareReset();
183         MySignals_BLE.initialize_BLE_values();
184
185     }
186     printf ("end access \n");
187     #endif
188     return ret;
189 }
190
191 uint8_t startBloodPressure() {
192     memset(&bloodPressureBLEData, 0x00, sizeof(bloodPressureBLEDataVector));
193 #if USE_MYSIGNALS
194     uint8_t ret = initMySignals();
195     if(ret == 1) {
196         ret = getMySignalBloodPressure();
197     }
198     return ret;
199 #else
200     return 0;
201 #endif
202 }