48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/iot/run.rb', line 48
def generate_template_service
yaml = load_yaml_body
if yaml.empty?
puts "No Yaml file"
return
end
str = ""
str << "#ifndef INCLUDED_MBED\n"
str << "#include \"mbed.h\"\n"
str << "#endif\n"
str << "#ifndef INCLUDED_BLEDEVICE\n"
str << "#include \"BLEDevice.h\"\n"
str << "#endif\n"
deviceinfo = yaml["deviceinfo"]
services = deviceinfo["services"]
services.each do |service|
service_name = service["name"]
chars = service["chars"]
chars.each do |characteristic|
char_uuid_str = characteristic["uuid"].gsub("-", "")
char_uuid = []
(0..15).each do |num|
char_uuid << char_uuid_str[num*2..(num*2+1)]
end
char_name = characteristic["name"]
str << "static const uint8_t #{char_name}_uuid[16] = {"
char_uuid.each do |temp|
str << "0x#{temp}, "
end
str << "};\n"
str << "uint8_t #{char_name}_value[8] = {0,};\n"
str << "GattCharacteristic #{char_name}(\n"
str << "#{char_name}_uuid,\n"
str << "#{char_name}_value,\n"
str << "sizeof(#{char_name}_value),\n"
str << "sizeof(#{char_name}_value),\n"
gatt_properties = []
properties = characteristic["properties"]
if properties.include? "read"
gatt_properties << "GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ"
end
if properties.include? "write"
gatt_properties << "GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE"
end
if properties.include? "notify"
gatt_properties << "GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY"
end
gatt_properties.each do |temp|
str << "#{temp} |"
end
str << ");\n"
str.gsub!("|)", ")")
end
str << "GattCharacteristic *#{service_name}_chars[] = {\n"
chars.each do |characteristic|
str << "&#{characteristic["name"]},\n"
end
str << "};\n"
service_uuid_str = service["uuid"].gsub("-", "")
service_uuid = []
(0..15).each do |num|
service_uuid << service_uuid_str[num*2..(num*2+1)]
end
service_name = service["name"]
str << "static const uint8_t #{service_name}_uuid[] = {\n"
service_uuid.each do |temp|
str << "0x#{temp}, "
end
str << "};\n"
str << "GattService #{service_name}(\n"
str << "#{service_name}_uuid,\n"
str << "#{service_name}_chars,\n"
str << "sizeof(#{service_name}_chars) / sizeof(GattCharacteristic *)\n"
str << ");\n"
end
str << "GattService *GattServices[]={\n"
services.each do |service|
str << "&#{service["name"]},"
end
str << "};"
File.write("./iot/STService.h", str)
end
|