generic_server.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  1. /* Bluetooth: Mesh Generic Server Models
  2. *
  3. * Copyright (c) 2018 Vikrant More
  4. * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. #include <errno.h>
  9. #include "access.h"
  10. #include "transport.h"
  11. #include "model_opcode.h"
  12. #include "state_transition.h"
  13. #include "device_property.h"
  14. #include "btc_ble_mesh_generic_model.h"
  15. static bt_mesh_mutex_t generic_server_lock;
  16. static void bt_mesh_generic_server_mutex_new(void)
  17. {
  18. if (!generic_server_lock.mutex) {
  19. bt_mesh_mutex_create(&generic_server_lock);
  20. }
  21. }
  22. static void bt_mesh_generic_server_mutex_free(void)
  23. {
  24. bt_mesh_mutex_free(&generic_server_lock);
  25. }
  26. void bt_mesh_generic_server_lock(void)
  27. {
  28. bt_mesh_mutex_lock(&generic_server_lock);
  29. }
  30. void bt_mesh_generic_server_unlock(void)
  31. {
  32. bt_mesh_mutex_unlock(&generic_server_lock);
  33. }
  34. /* message handlers (Start) */
  35. /* Generic OnOff Server message handlers */
  36. static void send_gen_onoff_status(struct bt_mesh_model *model,
  37. struct bt_mesh_msg_ctx *ctx,
  38. bool publish)
  39. {
  40. struct bt_mesh_gen_onoff_srv *srv = model->user_data;
  41. struct net_buf_simple *msg = NULL;
  42. u8_t length = 2 + 3;
  43. if (ctx == NULL && publish == false) {
  44. BT_ERR("%s, Invalid parameter", __func__);
  45. return;
  46. }
  47. if (publish == false) {
  48. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  49. if (msg == NULL) {
  50. BT_ERR("%s, Failed to allocate memory", __func__);
  51. return;
  52. }
  53. } else {
  54. msg = bt_mesh_server_get_pub_msg(model, length);
  55. if (msg == NULL) {
  56. return;
  57. }
  58. }
  59. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS);
  60. net_buf_simple_add_u8(msg, srv->state.onoff);
  61. if (srv->transition.counter) {
  62. bt_mesh_server_calc_remain_time(&srv->transition);
  63. net_buf_simple_add_u8(msg, srv->state.target_onoff);
  64. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  65. }
  66. if (publish == false) {
  67. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  68. bt_mesh_free_buf(msg);
  69. } else {
  70. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  71. }
  72. return;
  73. }
  74. static void gen_onoff_get(struct bt_mesh_model *model,
  75. struct bt_mesh_msg_ctx *ctx,
  76. struct net_buf_simple *buf)
  77. {
  78. struct bt_mesh_gen_onoff_srv *srv = model->user_data;
  79. if (srv == NULL) {
  80. BT_ERR("%s, Invalid model user_data", __func__);
  81. return;
  82. }
  83. /* Callback the received message to the application layer */
  84. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  85. bt_mesh_generic_server_cb_evt_to_btc(
  86. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  87. return;
  88. }
  89. send_gen_onoff_status(model, ctx, false);
  90. return;
  91. }
  92. void gen_onoff_publish(struct bt_mesh_model *model)
  93. {
  94. if (model->user_data == NULL) {
  95. BT_ERR("%s, Invalid model user_data", __func__);
  96. return;
  97. }
  98. send_gen_onoff_status(model, NULL, true);
  99. return;
  100. }
  101. static void gen_onoff_set(struct bt_mesh_model *model,
  102. struct bt_mesh_msg_ctx *ctx,
  103. struct net_buf_simple *buf)
  104. {
  105. struct bt_mesh_gen_onoff_srv *srv = model->user_data;
  106. u8_t tid = 0U, onoff = 0U, trans_time = 0U, delay = 0U;
  107. bool optional = false;
  108. s64_t now = 0;
  109. if (srv == NULL) {
  110. BT_ERR("%s, Invalid model user_data", __func__);
  111. return;
  112. }
  113. onoff = net_buf_simple_pull_u8(buf);
  114. if (onoff > BLE_MESH_STATE_ON) {
  115. BT_ERR("%s, Invalid OnOff value 0x%02x", __func__, onoff);
  116. return;
  117. }
  118. tid = net_buf_simple_pull_u8(buf);
  119. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  120. return;
  121. }
  122. /* Callback the received message to the application layer */
  123. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  124. bt_mesh_gen_server_recv_set_msg_t set = {
  125. .onoff_set.op_en = optional,
  126. .onoff_set.onoff = onoff,
  127. .onoff_set.tid = tid,
  128. .onoff_set.trans_time = trans_time,
  129. .onoff_set.delay = delay,
  130. };
  131. bt_mesh_generic_server_cb_evt_to_btc(
  132. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  133. return;
  134. }
  135. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  136. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONOFF_SET) {
  137. send_gen_onoff_status(model, ctx, false);
  138. }
  139. send_gen_onoff_status(model, NULL, true);
  140. /* In this condition, no event will be callback to application layer */
  141. return;
  142. }
  143. bt_mesh_generic_server_lock();
  144. bt_mesh_server_stop_transition(&srv->transition);
  145. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  146. srv->state.target_onoff = onoff;
  147. if (srv->state.target_onoff != srv->state.onoff) {
  148. generic_onoff_tt_values(srv, trans_time, delay);
  149. } else {
  150. bt_mesh_gen_server_state_change_t change = {
  151. .gen_onoff_set.onoff = srv->state.onoff,
  152. };
  153. bt_mesh_generic_server_cb_evt_to_btc(
  154. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  155. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONOFF_SET) {
  156. send_gen_onoff_status(model, ctx, false);
  157. }
  158. send_gen_onoff_status(model, NULL, true);
  159. bt_mesh_generic_server_unlock();
  160. return;
  161. }
  162. /* Copy the ctx of the received message */
  163. if (srv->transition.timer.work._reserved) {
  164. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  165. }
  166. /* For Instantaneous Transition */
  167. if (srv->transition.counter == 0U) {
  168. srv->state.onoff = srv->state.target_onoff;
  169. }
  170. srv->transition.just_started = true;
  171. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONOFF_SET) {
  172. send_gen_onoff_status(model, ctx, false);
  173. }
  174. send_gen_onoff_status(model, NULL, true);
  175. bt_mesh_generic_server_unlock();
  176. bt_mesh_server_start_transition(&srv->transition);
  177. return;
  178. }
  179. /* Generic Level Server message handlers */
  180. static void send_gen_level_status(struct bt_mesh_model *model,
  181. struct bt_mesh_msg_ctx *ctx,
  182. bool publish)
  183. {
  184. struct bt_mesh_gen_level_srv *srv = model->user_data;
  185. struct net_buf_simple *msg = NULL;
  186. u8_t length = 2 + 5;
  187. if (ctx == NULL && publish == false) {
  188. BT_ERR("%s, Invalid parameter", __func__);
  189. return;
  190. }
  191. if (publish == false) {
  192. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  193. if (msg == NULL) {
  194. BT_ERR("%s, Failed to allocate memory", __func__);
  195. return;
  196. }
  197. } else {
  198. msg = bt_mesh_server_get_pub_msg(model, length);
  199. if (msg == NULL) {
  200. return;
  201. }
  202. }
  203. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS);
  204. net_buf_simple_add_le16(msg, srv->state.level);
  205. if (srv->transition.counter) {
  206. if (srv->state.move_start) {
  207. if (srv->state.positive) {
  208. net_buf_simple_add_le16(msg, INT16_MAX);
  209. } else { /* 0 should not be possible */
  210. net_buf_simple_add_le16(msg, INT16_MIN);
  211. }
  212. net_buf_simple_add_u8(msg, BLE_MESH_UNKNOWN_REMAIN_TIME);
  213. } else {
  214. bt_mesh_server_calc_remain_time(&srv->transition);
  215. net_buf_simple_add_le16(msg, srv->state.target_level);
  216. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  217. }
  218. }
  219. if (publish == false) {
  220. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  221. bt_mesh_free_buf(msg);
  222. } else {
  223. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  224. }
  225. return;
  226. }
  227. static void gen_level_get(struct bt_mesh_model *model,
  228. struct bt_mesh_msg_ctx *ctx,
  229. struct net_buf_simple *buf)
  230. {
  231. struct bt_mesh_gen_level_srv *srv = model->user_data;
  232. if (srv == NULL) {
  233. BT_ERR("%s, Invalid model user_data", __func__);
  234. return;
  235. }
  236. /* Callback the received message to the application layer */
  237. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  238. bt_mesh_generic_server_cb_evt_to_btc(
  239. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  240. return;
  241. }
  242. send_gen_level_status(model, ctx, false);
  243. return;
  244. }
  245. void gen_level_publish(struct bt_mesh_model *model)
  246. {
  247. if (model->user_data == NULL) {
  248. BT_ERR("%s, Invalid model user_data", __func__);
  249. return;
  250. }
  251. send_gen_level_status(model, NULL, true);
  252. return;
  253. }
  254. static void gen_level_set(struct bt_mesh_model *model,
  255. struct bt_mesh_msg_ctx *ctx,
  256. struct net_buf_simple *buf)
  257. {
  258. struct bt_mesh_gen_level_srv *srv = model->user_data;
  259. u8_t tid = 0U, trans_time = 0U, delay = 0U;
  260. bool optional = false;
  261. s16_t level = 0;
  262. s64_t now = 0;
  263. if (srv == NULL) {
  264. BT_ERR("%s, Invalid model user_data", __func__);
  265. return;
  266. }
  267. level = (s16_t) net_buf_simple_pull_le16(buf);
  268. tid = net_buf_simple_pull_u8(buf);
  269. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  270. return;
  271. }
  272. /* Callback the received message to the application layer */
  273. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  274. bt_mesh_gen_server_recv_set_msg_t set = {
  275. .level_set.op_en = optional,
  276. .level_set.level = level,
  277. .level_set.tid = tid,
  278. .level_set.trans_time = trans_time,
  279. .level_set.delay = delay,
  280. };
  281. bt_mesh_generic_server_cb_evt_to_btc(
  282. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  283. return;
  284. }
  285. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  286. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LEVEL_SET) {
  287. send_gen_level_status(model, ctx, false);
  288. }
  289. send_gen_level_status(model, NULL, true);
  290. /* In this condition, no event will be callback to application layer */
  291. return;
  292. }
  293. bt_mesh_generic_server_lock();
  294. bt_mesh_server_stop_transition(&srv->transition);
  295. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  296. srv->state.target_level = level;
  297. /**
  298. * If the target state is equal to the current state, the transition
  299. * shall not be started and is considered complete.
  300. */
  301. if (srv->state.target_level != srv->state.level) {
  302. generic_level_tt_values(srv, trans_time, delay);
  303. } else {
  304. bt_mesh_gen_server_state_change_t change = {
  305. .gen_level_set.level = srv->state.level,
  306. };
  307. bt_mesh_generic_server_cb_evt_to_btc(
  308. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  309. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LEVEL_SET) {
  310. send_gen_level_status(model, ctx, false);
  311. }
  312. send_gen_level_status(model, NULL, true);
  313. bt_mesh_generic_server_unlock();
  314. return;
  315. }
  316. /* Copy the ctx of the received message */
  317. if (srv->transition.timer.work._reserved) {
  318. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  319. }
  320. /* For Instantaneous Transition */
  321. if (srv->transition.counter == 0U) {
  322. srv->state.level = srv->state.target_level;
  323. }
  324. srv->transition.just_started = true;
  325. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LEVEL_SET) {
  326. send_gen_level_status(model, ctx, false);
  327. }
  328. send_gen_level_status(model, NULL, true);
  329. bt_mesh_generic_server_unlock();
  330. bt_mesh_server_start_transition(&srv->transition);
  331. return;
  332. }
  333. static void gen_delta_set(struct bt_mesh_model *model,
  334. struct bt_mesh_msg_ctx *ctx,
  335. struct net_buf_simple *buf)
  336. {
  337. struct bt_mesh_gen_level_srv *srv = model->user_data;
  338. u8_t tid = 0U, trans_time = 0U, delay = 0U;
  339. s32_t tmp32 = 0, delta = 0;
  340. bool optional = false;
  341. s64_t now = 0;
  342. if (srv == NULL) {
  343. BT_ERR("%s, Invalid model user_data", __func__);
  344. return;
  345. }
  346. delta = (s32_t)net_buf_simple_pull_le32(buf);
  347. tid = net_buf_simple_pull_u8(buf);
  348. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  349. return;
  350. }
  351. /* Callback the received message to the application layer */
  352. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  353. bt_mesh_gen_server_recv_set_msg_t set = {
  354. .delta_set.op_en = optional,
  355. .delta_set.delta_level = delta,
  356. .delta_set.tid = tid,
  357. .delta_set.trans_time = trans_time,
  358. .delta_set.delay = delay,
  359. };
  360. bt_mesh_generic_server_cb_evt_to_btc(
  361. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  362. return;
  363. }
  364. /**
  365. * A number of Generic Delta Set and Generic Delta Set Unacknowledged
  366. * messages with the same transaction identifier set in the TID field
  367. * may be sent.
  368. *
  369. * A new transaction starts when the TID field value in the received
  370. * message is different from the TID field value in the previously
  371. * received message that was using the same source and destination
  372. * addresses or from the most recently received message with the same
  373. * TID field value that was received 6 or more seconds earlier.
  374. */
  375. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  376. if (srv->state.last_delta == delta) {
  377. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) {
  378. send_gen_level_status(model, ctx, false);
  379. }
  380. send_gen_level_status(model, NULL, true);
  381. /* In this condition, no event will be callback to application layer */
  382. return;
  383. }
  384. tmp32 = srv->state.last_level + delta;
  385. } else {
  386. /* Starts a new transaction */
  387. srv->state.last_level = srv->state.level;
  388. tmp32 = srv->state.level + delta;
  389. }
  390. bt_mesh_generic_server_lock();
  391. bt_mesh_server_stop_transition(&srv->transition);
  392. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  393. srv->state.last_delta = delta;
  394. if (tmp32 < INT16_MIN) {
  395. tmp32 = INT16_MIN;
  396. } else if (tmp32 > INT16_MAX) {
  397. tmp32 = INT16_MAX;
  398. }
  399. srv->state.target_level = tmp32;
  400. /**
  401. * If the target state is equal to the current state, the transition
  402. * shall not be started and is considered complete.
  403. */
  404. if (srv->state.target_level != srv->state.level) {
  405. generic_level_tt_values(srv, trans_time, delay);
  406. } else {
  407. bt_mesh_gen_server_state_change_t change = {
  408. .gen_delta_set.level = srv->state.level,
  409. };
  410. bt_mesh_generic_server_cb_evt_to_btc(
  411. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  412. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) {
  413. send_gen_level_status(model, ctx, false);
  414. }
  415. send_gen_level_status(model, NULL, true);
  416. bt_mesh_generic_server_unlock();
  417. return;
  418. }
  419. /* Copy the ctx of the received message */
  420. if (srv->transition.timer.work._reserved) {
  421. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  422. }
  423. /* For Instantaneous Transition */
  424. if (srv->transition.counter == 0U) {
  425. srv->state.level = srv->state.target_level;
  426. }
  427. srv->transition.just_started = true;
  428. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) {
  429. send_gen_level_status(model, ctx, false);
  430. }
  431. send_gen_level_status(model, NULL, true);
  432. bt_mesh_generic_server_unlock();
  433. bt_mesh_server_start_transition(&srv->transition);
  434. return;
  435. }
  436. static void gen_move_set(struct bt_mesh_model *model,
  437. struct bt_mesh_msg_ctx *ctx,
  438. struct net_buf_simple *buf)
  439. {
  440. struct bt_mesh_gen_level_srv *srv = model->user_data;
  441. u8_t tid = 0U, trans_time = 0U, delay = 0U;
  442. bool optional = false;
  443. s16_t delta = 0;
  444. s32_t tmp32 = 0;
  445. s64_t now = 0;
  446. if (srv == NULL) {
  447. BT_ERR("%s, Invalid model user_data", __func__);
  448. return;
  449. }
  450. delta = (s16_t) net_buf_simple_pull_le16(buf);
  451. tid = net_buf_simple_pull_u8(buf);
  452. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  453. return;
  454. }
  455. /* Callback the received message to the application layer */
  456. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  457. bt_mesh_gen_server_recv_set_msg_t set = {
  458. .move_set.op_en = optional,
  459. .move_set.delta_level = delta,
  460. .move_set.tid = tid,
  461. .move_set.trans_time = trans_time,
  462. .move_set.delay = delay,
  463. };
  464. bt_mesh_generic_server_cb_evt_to_btc(
  465. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  466. return;
  467. }
  468. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  469. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MOVE_SET) {
  470. send_gen_level_status(model, ctx, false);
  471. }
  472. send_gen_level_status(model, NULL, true);
  473. /* In this condition, no event will be callback to application layer */
  474. return;
  475. }
  476. bt_mesh_generic_server_lock();
  477. bt_mesh_server_stop_transition(&srv->transition);
  478. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  479. srv->state.last_delta = delta;
  480. tmp32 = srv->state.level + delta;
  481. if (tmp32 < INT16_MIN) {
  482. tmp32 = INT16_MIN;
  483. } else if (tmp32 > INT16_MAX) {
  484. tmp32 = INT16_MAX;
  485. }
  486. srv->state.target_level = tmp32;
  487. /**
  488. * If the target state is equal to the current state, the transition
  489. * shall not be started and is considered complete.
  490. */
  491. if (srv->state.target_level != srv->state.level) {
  492. generic_level_tt_values(srv, trans_time, delay);
  493. } else {
  494. bt_mesh_gen_server_state_change_t change = {
  495. .gen_move_set.level = srv->state.level,
  496. };
  497. bt_mesh_generic_server_cb_evt_to_btc(
  498. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  499. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MOVE_SET) {
  500. send_gen_level_status(model, ctx, false);
  501. }
  502. send_gen_level_status(model, NULL, true);
  503. srv->state.move_start = false;
  504. bt_mesh_generic_server_unlock();
  505. return;
  506. }
  507. /* Copy the ctx of the received message */
  508. if (srv->transition.timer.work._reserved) {
  509. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  510. }
  511. if (delta) {
  512. srv->state.move_start = true;
  513. srv->state.positive = (delta > 0) ? true : false;
  514. }
  515. srv->transition.just_started = true;
  516. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MOVE_SET) {
  517. send_gen_level_status(model, ctx, false);
  518. }
  519. send_gen_level_status(model, NULL, true);
  520. bt_mesh_generic_server_unlock();
  521. /**
  522. * If (trans_time == 0) OR (delta == 0)
  523. * 1. If the resulting Transition Time is equal to 0 or is undefined,
  524. * the Generic Move Set command will not initiate any Generic Level
  525. * state change.
  526. * 2. When a Generic Level Server receives the message with a value of
  527. * the Delta Level field equal to 0, it shall stop changing the
  528. * Generic Level state. (if delta == 0, srv->state.target_level will
  529. * equal to srv->state.level)
  530. */
  531. if (srv->transition.counter == 0U) {
  532. srv->state.move_start = false;
  533. bt_mesh_gen_server_state_change_t change = {
  534. .gen_move_set.level = srv->state.level,
  535. };
  536. bt_mesh_generic_server_cb_evt_to_btc(
  537. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  538. return;
  539. }
  540. bt_mesh_server_start_transition(&srv->transition);
  541. return;
  542. }
  543. /* Generic Default Transition Time Server message handlers */
  544. static void send_gen_def_trans_time_status(struct bt_mesh_model *model,
  545. struct bt_mesh_msg_ctx *ctx,
  546. bool publish)
  547. {
  548. struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data;
  549. struct net_buf_simple *msg = NULL;
  550. u8_t length = 2 + 1;
  551. if (ctx == NULL && publish == false) {
  552. BT_ERR("%s, Invalid parameter", __func__);
  553. return;
  554. }
  555. if (publish == false) {
  556. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  557. if (msg == NULL) {
  558. BT_ERR("%s, Failed to allocate memory", __func__);
  559. return;
  560. }
  561. } else {
  562. msg = bt_mesh_server_get_pub_msg(model, length);
  563. if (msg == NULL) {
  564. return;
  565. }
  566. }
  567. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS);
  568. net_buf_simple_add_u8(msg, srv->state.trans_time);
  569. if (publish == false) {
  570. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  571. bt_mesh_free_buf(msg);
  572. } else {
  573. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  574. }
  575. return;
  576. }
  577. static void gen_def_trans_time_get(struct bt_mesh_model *model,
  578. struct bt_mesh_msg_ctx *ctx,
  579. struct net_buf_simple *buf)
  580. {
  581. struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data;
  582. if (srv == NULL) {
  583. BT_ERR("%s, Invalid model user_data", __func__);
  584. return;
  585. }
  586. /* Callback the received message to the application layer */
  587. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  588. bt_mesh_generic_server_cb_evt_to_btc(
  589. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  590. return;
  591. }
  592. send_gen_def_trans_time_status(model, ctx, false);
  593. return;
  594. }
  595. static void gen_def_trans_time_set(struct bt_mesh_model *model,
  596. struct bt_mesh_msg_ctx *ctx,
  597. struct net_buf_simple *buf)
  598. {
  599. struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data;
  600. u8_t trans_time = 0U;
  601. if (srv == NULL) {
  602. BT_ERR("%s, Invalid model user_data", __func__);
  603. return;
  604. }
  605. trans_time = net_buf_simple_pull_u8(buf);
  606. if ((trans_time & 0x3F) == 0x3F) {
  607. BT_WARN("%s, Invalid Transaction Number of Steps 0x3F", __func__);
  608. return;
  609. }
  610. /* Callback the received message to the application layer */
  611. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  612. bt_mesh_gen_server_recv_set_msg_t set = {
  613. .def_trans_time_set.trans_time = trans_time,
  614. };
  615. bt_mesh_generic_server_cb_evt_to_btc(
  616. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  617. return;
  618. }
  619. if (srv->state.trans_time != trans_time) {
  620. srv->state.trans_time = trans_time;
  621. }
  622. bt_mesh_gen_server_state_change_t change = {
  623. .gen_def_trans_time_set.trans_time = trans_time,
  624. };
  625. bt_mesh_generic_server_cb_evt_to_btc(
  626. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  627. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET) {
  628. send_gen_def_trans_time_status(model, ctx, false);
  629. }
  630. send_gen_def_trans_time_status(model, NULL, true);
  631. return;
  632. }
  633. /* Generic Power OnOff Server message handlers */
  634. static void send_gen_onpowerup_status(struct bt_mesh_model *model,
  635. struct bt_mesh_msg_ctx *ctx,
  636. bool publish)
  637. {
  638. struct net_buf_simple *msg = NULL;
  639. u8_t length = 2 + 1;
  640. if (ctx == NULL && publish == false) {
  641. BT_ERR("%s, Invalid parameter", __func__);
  642. return;
  643. }
  644. if (publish == false) {
  645. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  646. if (msg == NULL) {
  647. BT_ERR("%s, Failed to allocate memory", __func__);
  648. return;
  649. }
  650. } else {
  651. msg = bt_mesh_server_get_pub_msg(model, length);
  652. if (msg == NULL) {
  653. return;
  654. }
  655. }
  656. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS);
  657. switch (model->id) {
  658. case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: {
  659. struct bt_mesh_gen_power_onoff_srv *srv = model->user_data;
  660. net_buf_simple_add_u8(msg, srv->state->onpowerup);
  661. break;
  662. }
  663. case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: {
  664. struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data;
  665. net_buf_simple_add_u8(msg, srv->state->onpowerup);
  666. break;
  667. }
  668. default:
  669. BT_ERR("%s, Invalid Generic Power OnOff Server 0x%04x", __func__, model->id);
  670. if (publish == false) {
  671. bt_mesh_free_buf(msg);
  672. }
  673. return;
  674. }
  675. if (publish == false) {
  676. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  677. bt_mesh_free_buf(msg);
  678. } else {
  679. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  680. }
  681. return;
  682. }
  683. static void gen_onpowerup_get(struct bt_mesh_model *model,
  684. struct bt_mesh_msg_ctx *ctx,
  685. struct net_buf_simple *buf)
  686. {
  687. struct bt_mesh_gen_power_onoff_srv *srv = model->user_data;
  688. if (srv == NULL || srv->state == NULL) {
  689. BT_ERR("%s, Invalid model user_data", __func__);
  690. return;
  691. }
  692. /* Callback the received message to the application layer */
  693. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  694. bt_mesh_generic_server_cb_evt_to_btc(
  695. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  696. return;
  697. }
  698. send_gen_onpowerup_status(model, ctx, false);
  699. return;
  700. }
  701. /* Generic Power OnOff Setup Server message handlers */
  702. void gen_onpowerup_publish(struct bt_mesh_model *model)
  703. {
  704. if (model->user_data == NULL) {
  705. BT_ERR("%s, Invalid model user_data", __func__);
  706. return;
  707. }
  708. switch (model->id) {
  709. case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: {
  710. struct bt_mesh_gen_power_onoff_srv *srv = model->user_data;
  711. if (srv->state == NULL) {
  712. BT_ERR("%s, Invalid Generic Power OnOff Server state", __func__);
  713. return;
  714. }
  715. break;
  716. }
  717. case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: {
  718. struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data;
  719. if (srv->state == NULL) {
  720. BT_ERR("%s, Invalid Generic Power OnOff Setup Server state", __func__);
  721. return;
  722. }
  723. break;
  724. }
  725. default:
  726. BT_ERR("%s, Invalid Generic Power OnOff Server 0x%04x", __func__, model->id);
  727. return;
  728. }
  729. send_gen_onpowerup_status(model, NULL, true);
  730. return;
  731. }
  732. static void gen_onpowerup_set(struct bt_mesh_model *model,
  733. struct bt_mesh_msg_ctx *ctx,
  734. struct net_buf_simple *buf)
  735. {
  736. struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data;
  737. u8_t onpowerup = 0U;
  738. if (srv == NULL || srv->state == NULL) {
  739. BT_ERR("%s, Invalid model user_data", __func__);
  740. return;
  741. }
  742. onpowerup = net_buf_simple_pull_u8(buf);
  743. if (onpowerup > BLE_MESH_STATE_RESTORE) {
  744. BT_WARN("%s, Invalid OnPowerUp value 0x%02x", __func__, onpowerup);
  745. return;
  746. }
  747. /* Callback the received message to the application layer */
  748. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  749. bt_mesh_gen_server_recv_set_msg_t set = {
  750. .onpowerup_set.onpowerup = onpowerup,
  751. };
  752. bt_mesh_generic_server_cb_evt_to_btc(
  753. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  754. return;
  755. }
  756. if (srv->state->onpowerup != onpowerup) {
  757. srv->state->onpowerup = onpowerup;
  758. }
  759. bt_mesh_gen_server_state_change_t change = {
  760. .gen_onpowerup_set.onpowerup = onpowerup,
  761. };
  762. bt_mesh_generic_server_cb_evt_to_btc(
  763. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  764. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET) {
  765. send_gen_onpowerup_status(model, ctx, false);
  766. }
  767. send_gen_onpowerup_status(model, NULL, true);
  768. return;
  769. }
  770. /* Generic Power Level Server message handlers */
  771. static void send_gen_power_level_status(struct bt_mesh_model *model,
  772. struct bt_mesh_msg_ctx *ctx,
  773. bool publish, u16_t opcode)
  774. {
  775. struct net_buf_simple *msg = NULL;
  776. u8_t length = 2 + 5;
  777. if (ctx == NULL && publish == false) {
  778. BT_ERR("%s, Invalid parameter", __func__);
  779. return;
  780. }
  781. if (publish == false) {
  782. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  783. if (msg == NULL) {
  784. BT_ERR("%s, Failed to allocate memory", __func__);
  785. return;
  786. }
  787. } else {
  788. msg = bt_mesh_server_get_pub_msg(model, length);
  789. if (msg == NULL) {
  790. return;
  791. }
  792. }
  793. bt_mesh_model_msg_init(msg, opcode);
  794. switch (opcode) {
  795. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS:
  796. case BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS: {
  797. struct bt_mesh_gen_power_level_srv *srv = model->user_data;
  798. if (opcode == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS) {
  799. net_buf_simple_add_le16(msg, srv->state->power_actual);
  800. if (srv->transition.counter) {
  801. bt_mesh_server_calc_remain_time(&srv->transition);
  802. net_buf_simple_add_le16(msg, srv->state->target_power_actual);
  803. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  804. }
  805. } else if (opcode == BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS) {
  806. net_buf_simple_add_le16(msg, srv->state->power_last);
  807. }
  808. break;
  809. }
  810. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS:
  811. if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV) {
  812. struct bt_mesh_gen_power_level_srv *srv = model->user_data;
  813. net_buf_simple_add_le16(msg, srv->state->power_default);
  814. } else if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV) {
  815. struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
  816. net_buf_simple_add_le16(msg, srv->state->power_default);
  817. }
  818. break;
  819. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS:
  820. if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV) {
  821. struct bt_mesh_gen_power_level_srv *srv = model->user_data;
  822. net_buf_simple_add_u8(msg, srv->state->status_code);
  823. net_buf_simple_add_le16(msg, srv->state->power_range_min);
  824. net_buf_simple_add_le16(msg, srv->state->power_range_max);
  825. } else if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV) {
  826. struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
  827. net_buf_simple_add_u8(msg, srv->state->status_code);
  828. net_buf_simple_add_le16(msg, srv->state->power_range_min);
  829. net_buf_simple_add_le16(msg, srv->state->power_range_max);
  830. }
  831. break;
  832. default:
  833. BT_WARN("%s, Unknown Generic Power status opcode 0x%04x", __func__, opcode);
  834. if (publish == false) {
  835. bt_mesh_free_buf(msg);
  836. }
  837. return;
  838. }
  839. if (publish == false) {
  840. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  841. bt_mesh_free_buf(msg);
  842. } else {
  843. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  844. }
  845. return;
  846. }
  847. static void gen_power_level_get(struct bt_mesh_model *model,
  848. struct bt_mesh_msg_ctx *ctx,
  849. struct net_buf_simple *buf)
  850. {
  851. struct bt_mesh_gen_power_level_srv *srv = model->user_data;
  852. u16_t opcode = 0U;
  853. if (srv == NULL || srv->state == NULL) {
  854. BT_ERR("%s, Invalid model user_data", __func__);
  855. return;
  856. }
  857. /* Callback the received message to the application layer */
  858. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  859. bt_mesh_generic_server_cb_evt_to_btc(
  860. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  861. return;
  862. }
  863. switch (ctx->recv_op) {
  864. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET:
  865. opcode = BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS;
  866. break;
  867. case BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET:
  868. opcode = BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS;
  869. break;
  870. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET:
  871. opcode = BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS;
  872. break;
  873. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET:
  874. opcode = BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS;
  875. break;
  876. default:
  877. BT_WARN("%s, Unknown Generic Power Get opcode 0x%04x", __func__, ctx->recv_op);
  878. return;
  879. }
  880. send_gen_power_level_status(model, ctx, false, opcode);
  881. return;
  882. }
  883. void gen_power_level_publish(struct bt_mesh_model *model, u16_t opcode)
  884. {
  885. if (model->user_data == NULL) {
  886. BT_ERR("%s, Invalid model user_data", __func__);
  887. return;
  888. }
  889. switch (model->id) {
  890. case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: {
  891. struct bt_mesh_gen_power_level_srv *srv = model->user_data;
  892. if (srv->state == NULL) {
  893. BT_ERR("%s, Invalid Generic Power Level Server state", __func__);
  894. return;
  895. }
  896. break;
  897. }
  898. case ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV: {
  899. struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
  900. if (srv->state == NULL) {
  901. BT_ERR("%s, Invalid Generic Power Level Setup Server state", __func__);
  902. return;
  903. }
  904. break;
  905. }
  906. default:
  907. BT_ERR("%s, Invalid Generic Power Level Server 0x%04x", __func__, model->id);
  908. return;
  909. }
  910. send_gen_power_level_status(model, NULL, true, opcode);
  911. return;
  912. }
  913. static void gen_power_level_set(struct bt_mesh_model *model,
  914. struct bt_mesh_msg_ctx *ctx,
  915. struct net_buf_simple *buf)
  916. {
  917. struct bt_mesh_gen_power_level_srv *srv = model->user_data;
  918. u8_t tid = 0U, trans_time = 0U, delay = 0U;
  919. bool optional = false;
  920. u16_t power = 0U;
  921. s64_t now = 0;
  922. if (srv == NULL || srv->state == NULL) {
  923. BT_ERR("%s, Invalid model user_data", __func__);
  924. return;
  925. }
  926. power = net_buf_simple_pull_le16(buf);
  927. tid = net_buf_simple_pull_u8(buf);
  928. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  929. return;
  930. }
  931. /* Callback the received message to the application layer */
  932. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  933. bt_mesh_gen_server_recv_set_msg_t set = {
  934. .power_level_set.op_en = optional,
  935. .power_level_set.power = power,
  936. .power_level_set.tid = tid,
  937. .power_level_set.trans_time = trans_time,
  938. .power_level_set.delay = delay,
  939. };
  940. bt_mesh_generic_server_cb_evt_to_btc(
  941. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  942. return;
  943. }
  944. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  945. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET) {
  946. send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
  947. }
  948. send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
  949. /* In this condition, no event will be callback to application layer */
  950. return;
  951. }
  952. bt_mesh_generic_server_lock();
  953. bt_mesh_server_stop_transition(&srv->transition);
  954. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  955. if (power) {
  956. if (srv->state->power_range_min && power < srv->state->power_range_min) {
  957. power = srv->state->power_range_min;
  958. } else if (srv->state->power_range_max && power > srv->state->power_range_max) {
  959. power = srv->state->power_range_max;
  960. }
  961. }
  962. srv->state->target_power_actual = power;
  963. /* If the target state is equal to the current state, the transition
  964. * shall not be started and is considered complete.
  965. */
  966. if (srv->state->target_power_actual != srv->state->power_actual) {
  967. generic_power_level_tt_values(srv, trans_time, delay);
  968. } else {
  969. bt_mesh_gen_server_state_change_t change = {
  970. .gen_power_level_set.power = srv->state->power_actual,
  971. };
  972. bt_mesh_generic_server_cb_evt_to_btc(
  973. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  974. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET) {
  975. send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
  976. }
  977. send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
  978. bt_mesh_generic_server_unlock();
  979. return;
  980. }
  981. /* Copy the ctx of the received message */
  982. if (srv->transition.timer.work._reserved) {
  983. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  984. }
  985. /* For Instantaneous Transition */
  986. if (srv->transition.counter == 0U) {
  987. srv->state->power_actual = srv->state->target_power_actual;
  988. /* Whenever the Generic Power Actual state is changed to a non-zero value
  989. * as a result of a non-transactional message or a completed sequence of
  990. * transactional messages, the value of the Generic Power Last state shall
  991. * be set to the value of the Generic Power Actual state.
  992. */
  993. if (srv->state->power_actual) {
  994. srv->state->power_last = srv->state->power_actual;
  995. }
  996. }
  997. srv->transition.just_started = true;
  998. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET) {
  999. send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
  1000. }
  1001. send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
  1002. bt_mesh_generic_server_unlock();
  1003. bt_mesh_server_start_transition(&srv->transition);
  1004. return;
  1005. }
  1006. /* Generic Power Level Setup Server message handlers */
  1007. static void gen_power_default_set(struct bt_mesh_model *model,
  1008. struct bt_mesh_msg_ctx *ctx,
  1009. struct net_buf_simple *buf)
  1010. {
  1011. struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
  1012. u16_t power = 0U;
  1013. if (srv == NULL || srv->state == NULL) {
  1014. BT_ERR("%s, Invalid model user_data", __func__);
  1015. return;
  1016. }
  1017. power = net_buf_simple_pull_le16(buf);
  1018. /* Callback the received message to the application layer */
  1019. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1020. bt_mesh_gen_server_recv_set_msg_t set = {
  1021. .power_default_set.power = power, /* Just callback the actual recived value */
  1022. };
  1023. bt_mesh_generic_server_cb_evt_to_btc(
  1024. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  1025. return;
  1026. }
  1027. /**
  1028. * Value 0x0000 has a special meaning defined: use the value of the
  1029. * Generic Power Last state as the default value.
  1030. */
  1031. if (power == 0x0000) {
  1032. power = srv->state->power_last;
  1033. }
  1034. if (srv->state->power_default != power) {
  1035. srv->state->power_default = power;
  1036. }
  1037. bt_mesh_gen_server_state_change_t change = {
  1038. .gen_power_default_set.power = power,
  1039. };
  1040. bt_mesh_generic_server_cb_evt_to_btc(
  1041. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  1042. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET) {
  1043. send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS);
  1044. }
  1045. send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS);
  1046. return;
  1047. }
  1048. static void gen_power_range_set(struct bt_mesh_model *model,
  1049. struct bt_mesh_msg_ctx *ctx,
  1050. struct net_buf_simple *buf)
  1051. {
  1052. struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
  1053. u16_t range_min = 0U, range_max = 0U;
  1054. if (srv == NULL || srv->state == NULL) {
  1055. BT_ERR("%s, Invalid model user_data", __func__);
  1056. return;
  1057. }
  1058. range_min = net_buf_simple_pull_le16(buf);
  1059. range_max = net_buf_simple_pull_le16(buf);
  1060. if (range_min > range_max) {
  1061. BT_ERR("%s, Range Min 0x%04x is greater than Range Max 0x%04x",
  1062. __func__, range_min, range_max);
  1063. return;
  1064. }
  1065. /* Callback the received message to the application layer */
  1066. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1067. bt_mesh_gen_server_recv_set_msg_t set = {
  1068. .power_range_set.range_min = range_min,
  1069. .power_range_set.range_max = range_max,
  1070. };
  1071. bt_mesh_generic_server_cb_evt_to_btc(
  1072. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  1073. return;
  1074. }
  1075. if (range_min == 0x0000) {
  1076. srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN;
  1077. } else if (range_max == 0x0000) {
  1078. srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX;
  1079. } else {
  1080. srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
  1081. }
  1082. if (range_min && srv->state->power_range_min != range_min) {
  1083. srv->state->power_range_min = range_min;
  1084. }
  1085. if (range_max && srv->state->power_range_max != range_max) {
  1086. srv->state->power_range_max = range_max;
  1087. }
  1088. bt_mesh_gen_server_state_change_t change = {
  1089. .gen_power_range_set.range_min = srv->state->power_range_min,
  1090. .gen_power_range_set.range_max = srv->state->power_range_max,
  1091. };
  1092. bt_mesh_generic_server_cb_evt_to_btc(
  1093. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  1094. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET) {
  1095. send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS);
  1096. }
  1097. send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS);
  1098. return;
  1099. }
  1100. /* Generic Battery Server message handlers */
  1101. static void gen_battery_get(struct bt_mesh_model *model,
  1102. struct bt_mesh_msg_ctx *ctx,
  1103. struct net_buf_simple *buf)
  1104. {
  1105. struct bt_mesh_gen_battery_srv *srv = model->user_data;
  1106. NET_BUF_SIMPLE_DEFINE(msg, 2 + 8 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1107. if (srv == NULL) {
  1108. BT_ERR("%s, Invalid model user_data", __func__);
  1109. return;
  1110. }
  1111. /* Callback the received message to the application layer */
  1112. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1113. bt_mesh_generic_server_cb_evt_to_btc(
  1114. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  1115. return;
  1116. }
  1117. bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS);
  1118. net_buf_simple_add_le32(&msg, srv->state.time_to_discharge << 8 | srv->state.battery_level);
  1119. net_buf_simple_add_le32(&msg, srv->state.battery_flags << 24 | srv->state.time_to_charge);
  1120. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL));
  1121. return;
  1122. }
  1123. /* Generic Location Server message handlers */
  1124. static void send_gen_location_status(struct bt_mesh_model *model,
  1125. struct bt_mesh_msg_ctx *ctx,
  1126. bool publish, u16_t opcode)
  1127. {
  1128. struct net_buf_simple *msg = NULL;
  1129. u8_t length = 1 + 10;
  1130. if (ctx == NULL && publish == false) {
  1131. BT_ERR("%s, Invalid parameter", __func__);
  1132. return;
  1133. }
  1134. if (publish == false) {
  1135. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1136. if (msg == NULL) {
  1137. BT_ERR("%s, Failed to allocate memory", __func__);
  1138. return;
  1139. }
  1140. } else {
  1141. msg = bt_mesh_server_get_pub_msg(model, length);
  1142. if (msg == NULL) {
  1143. return;
  1144. }
  1145. }
  1146. bt_mesh_model_msg_init(msg, opcode);
  1147. switch (opcode) {
  1148. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS:
  1149. if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SRV) {
  1150. struct bt_mesh_gen_location_srv *srv = model->user_data;
  1151. net_buf_simple_add_le32(msg, srv->state->global_latitude);
  1152. net_buf_simple_add_le32(msg, srv->state->global_longitude);
  1153. net_buf_simple_add_le16(msg, srv->state->global_altitude);
  1154. } else if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV) {
  1155. struct bt_mesh_gen_location_setup_srv *srv = model->user_data;
  1156. net_buf_simple_add_le32(msg, srv->state->global_latitude);
  1157. net_buf_simple_add_le32(msg, srv->state->global_longitude);
  1158. net_buf_simple_add_le16(msg, srv->state->global_altitude);
  1159. }
  1160. break;
  1161. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS:
  1162. if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SRV) {
  1163. struct bt_mesh_gen_location_srv *srv = model->user_data;
  1164. net_buf_simple_add_le16(msg, srv->state->local_north);
  1165. net_buf_simple_add_le16(msg, srv->state->local_east);
  1166. net_buf_simple_add_le16(msg, srv->state->local_altitude);
  1167. net_buf_simple_add_u8(msg, srv->state->floor_number);
  1168. net_buf_simple_add_le16(msg, srv->state->uncertainty);
  1169. } else if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV) {
  1170. struct bt_mesh_gen_location_setup_srv *srv = model->user_data;
  1171. net_buf_simple_add_le16(msg, srv->state->local_north);
  1172. net_buf_simple_add_le16(msg, srv->state->local_east);
  1173. net_buf_simple_add_le16(msg, srv->state->local_altitude);
  1174. net_buf_simple_add_u8(msg, srv->state->floor_number);
  1175. net_buf_simple_add_le16(msg, srv->state->uncertainty);
  1176. }
  1177. break;
  1178. default:
  1179. BT_WARN("%s, Unknown Generic Location status opcode 0x%04x", __func__, opcode);
  1180. if (publish == false) {
  1181. bt_mesh_free_buf(msg);
  1182. }
  1183. return;
  1184. }
  1185. if (publish == false) {
  1186. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1187. bt_mesh_free_buf(msg);
  1188. } else {
  1189. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  1190. }
  1191. return;
  1192. }
  1193. static void gen_location_get(struct bt_mesh_model *model,
  1194. struct bt_mesh_msg_ctx *ctx,
  1195. struct net_buf_simple *buf)
  1196. {
  1197. struct bt_mesh_gen_location_srv *srv = model->user_data;
  1198. u16_t opcode = 0U;
  1199. if (srv == NULL || srv->state == NULL) {
  1200. BT_ERR("%s, Invalid model user_data", __func__);
  1201. return;
  1202. }
  1203. /* Callback the received message to the application layer */
  1204. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1205. bt_mesh_generic_server_cb_evt_to_btc(
  1206. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  1207. return;
  1208. }
  1209. switch (ctx->recv_op) {
  1210. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET:
  1211. opcode = BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS;
  1212. break;
  1213. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET:
  1214. opcode = BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS;
  1215. break;
  1216. default:
  1217. BT_WARN("%s, Unknown Generic Location Get opcode 0x%04x", __func__, ctx->recv_op);
  1218. return;
  1219. }
  1220. send_gen_location_status(model, ctx, false, opcode);
  1221. return;
  1222. }
  1223. /* Generic Location Setup Server message handlers */
  1224. static void gen_location_set(struct bt_mesh_model *model,
  1225. struct bt_mesh_msg_ctx *ctx,
  1226. struct net_buf_simple *buf)
  1227. {
  1228. struct bt_mesh_gen_location_setup_srv *srv = model->user_data;
  1229. u16_t opcode = 0U;
  1230. if (srv == NULL || srv->state == NULL) {
  1231. BT_ERR("%s, Invalid model user_data", __func__);
  1232. return;
  1233. }
  1234. switch (ctx->recv_op) {
  1235. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET:
  1236. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK: {
  1237. opcode = BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS;
  1238. s32_t latitude = net_buf_simple_pull_le32(buf);
  1239. s32_t longitude = net_buf_simple_pull_le32(buf);
  1240. s16_t altitude = net_buf_simple_pull_le16(buf);
  1241. /* Callback the received message to the application layer */
  1242. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1243. bt_mesh_gen_server_recv_set_msg_t set = {
  1244. .loc_global_set.latitude = latitude,
  1245. .loc_global_set.longitude = longitude,
  1246. .loc_global_set.altitude = altitude,
  1247. };
  1248. bt_mesh_generic_server_cb_evt_to_btc(
  1249. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  1250. return;
  1251. }
  1252. if (latitude != 0x80000000) {
  1253. srv->state->global_latitude = latitude;
  1254. }
  1255. if (longitude != 0x80000000) {
  1256. srv->state->global_longitude = longitude;
  1257. }
  1258. if (altitude != 0x7FFF) {
  1259. srv->state->global_altitude = altitude;
  1260. }
  1261. bt_mesh_gen_server_state_change_t change = {
  1262. .gen_loc_global_set.latitude = srv->state->global_latitude,
  1263. .gen_loc_global_set.longitude = srv->state->global_longitude,
  1264. .gen_loc_global_set.altitude = srv->state->global_altitude,
  1265. };
  1266. bt_mesh_generic_server_cb_evt_to_btc(
  1267. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  1268. break;
  1269. }
  1270. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET:
  1271. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK: {
  1272. opcode = BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS;
  1273. u16_t north = net_buf_simple_pull_le16(buf);
  1274. u16_t east = net_buf_simple_pull_le16(buf);
  1275. u16_t altitude = net_buf_simple_pull_le16(buf);
  1276. u8_t floor = net_buf_simple_pull_u8(buf);
  1277. u16_t uncertainty = net_buf_simple_pull_le16(buf);
  1278. /* Callback the received message to the application layer */
  1279. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1280. bt_mesh_gen_server_recv_set_msg_t set = {
  1281. .loc_local_set.north = north,
  1282. .loc_local_set.east = east,
  1283. .loc_local_set.altitude = altitude,
  1284. .loc_local_set.floor_number = floor,
  1285. .loc_local_set.uncertainty = uncertainty,
  1286. };
  1287. bt_mesh_generic_server_cb_evt_to_btc(
  1288. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  1289. return;
  1290. }
  1291. if (north != 0x8000) {
  1292. srv->state->local_north = north;
  1293. }
  1294. if (east != 0x8000) {
  1295. srv->state->local_east = east;
  1296. }
  1297. if (altitude != 0x7FFF) {
  1298. srv->state->local_altitude = altitude;
  1299. }
  1300. if (floor != 0xFF) {
  1301. srv->state->floor_number = floor;
  1302. }
  1303. srv->state->uncertainty = uncertainty;
  1304. bt_mesh_gen_server_state_change_t change = {
  1305. .gen_loc_local_set.north = srv->state->local_north,
  1306. .gen_loc_local_set.east = srv->state->local_east,
  1307. .gen_loc_local_set.altitude = srv->state->local_altitude,
  1308. .gen_loc_local_set.floor_number = srv->state->floor_number,
  1309. .gen_loc_local_set.uncertainty = srv->state->uncertainty,
  1310. };
  1311. bt_mesh_generic_server_cb_evt_to_btc(
  1312. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  1313. break;
  1314. }
  1315. default:
  1316. BT_WARN("%s, Unknown Generic Location Set opcode 0x%04x", __func__, ctx->recv_op);
  1317. return;
  1318. }
  1319. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET ||
  1320. ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET) {
  1321. send_gen_location_status(model, ctx, false, opcode);
  1322. }
  1323. send_gen_location_status(model, NULL, true, opcode);
  1324. return;
  1325. }
  1326. /* Generic User Property Server message handlers */
  1327. struct bt_mesh_generic_property *gen_get_user_property(struct bt_mesh_model *model,
  1328. u16_t property_id)
  1329. {
  1330. struct bt_mesh_gen_user_prop_srv *srv = model->user_data;
  1331. int i;
  1332. for (i = 0; i < srv->property_count; i++) {
  1333. if (srv->properties[i].id == property_id) {
  1334. return &srv->properties[i];
  1335. }
  1336. }
  1337. return NULL;
  1338. }
  1339. static void send_gen_user_prop_status(struct bt_mesh_model *model,
  1340. struct bt_mesh_msg_ctx *ctx,
  1341. u16_t property_id, bool publish)
  1342. {
  1343. struct bt_mesh_generic_property *property = NULL;
  1344. struct net_buf_simple *msg = NULL;
  1345. u16_t length = 0U;
  1346. if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) {
  1347. BT_ERR("%s, Invalid User Property ID 0x%04x", __func__, property_id);
  1348. return;
  1349. }
  1350. property = gen_get_user_property(model, property_id);
  1351. if (property == NULL) {
  1352. BT_WARN("%s, User property 0x%04x not exist", __func__, property_id);
  1353. length = sizeof(property_id);
  1354. } else {
  1355. length = sizeof(property->id) + sizeof(property->user_access) + property->val->len;
  1356. }
  1357. if (publish == false) {
  1358. msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1359. if (msg == NULL) {
  1360. BT_ERR("%s, Failed to allocate memory", __func__);
  1361. return;
  1362. }
  1363. } else {
  1364. msg = bt_mesh_server_get_pub_msg(model, 1 + length);
  1365. if (msg == NULL) {
  1366. return;
  1367. }
  1368. }
  1369. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS);
  1370. if (property == NULL) {
  1371. net_buf_simple_add_le16(msg, property_id);
  1372. } else {
  1373. net_buf_simple_add_le16(msg, property->id);
  1374. net_buf_simple_add_u8(msg, property->user_access);
  1375. if ((ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET &&
  1376. property->user_access != USER_ACCESS_PROHIBIT &&
  1377. property->user_access != USER_ACCESS_WRITE) ||
  1378. ((ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET ||
  1379. ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK) &&
  1380. property->user_access != USER_ACCESS_PROHIBIT &&
  1381. property->user_access != USER_ACCESS_READ)) {
  1382. net_buf_simple_add_mem(msg, property->val->data, property->val->len);
  1383. }
  1384. }
  1385. if (publish == false) {
  1386. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1387. bt_mesh_free_buf(msg);
  1388. } else {
  1389. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  1390. }
  1391. return;
  1392. }
  1393. static void gen_user_prop_get(struct bt_mesh_model *model,
  1394. struct bt_mesh_msg_ctx *ctx,
  1395. struct net_buf_simple *buf)
  1396. {
  1397. struct bt_mesh_gen_user_prop_srv *srv = model->user_data;
  1398. if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
  1399. BT_ERR("%s, Invalid model user_data", __func__);
  1400. return;
  1401. }
  1402. /* Callback the received message to the application layer */
  1403. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1404. /**
  1405. * Also we can use __packed for esp_ble_mesh_gen_user_property_get_t,
  1406. * and directly callback with buf->data & buf->len.
  1407. */
  1408. bt_mesh_gen_server_recv_get_msg_t get = {0};
  1409. const u8_t *param = NULL;
  1410. size_t len = 0;
  1411. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET) {
  1412. get.user_property_get.id = net_buf_simple_pull_le16(buf);
  1413. param = (const u8_t *)&get;
  1414. len = sizeof(get);
  1415. }
  1416. bt_mesh_generic_server_cb_evt_to_btc(
  1417. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, param, len);
  1418. return;
  1419. }
  1420. switch (ctx->recv_op) {
  1421. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET: {
  1422. struct net_buf_simple *msg = NULL;
  1423. u8_t i;
  1424. msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1425. if (msg == NULL) {
  1426. BT_ERR("%s, Failed to allocate memory", __func__);
  1427. return;
  1428. }
  1429. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS);
  1430. for (i = 0U; i < srv->property_count; i++) {
  1431. if (srv->properties[i].admin_access != ADMIN_NOT_USER_PROP &&
  1432. srv->properties[i].manu_access != MANU_NOT_USER_PROP) {
  1433. net_buf_simple_add_le16(msg, srv->properties[i].id);
  1434. }
  1435. }
  1436. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1437. bt_mesh_free_buf(msg);
  1438. return;
  1439. }
  1440. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: {
  1441. u16_t property_id = net_buf_simple_pull_le16(buf);
  1442. send_gen_user_prop_status(model, ctx, property_id, false);
  1443. return;
  1444. }
  1445. default:
  1446. BT_WARN("%s, Unknown Generic User Property Get opcode 0x%04x", __func__, ctx->recv_op);
  1447. return;
  1448. }
  1449. }
  1450. static void gen_user_prop_set(struct bt_mesh_model *model,
  1451. struct bt_mesh_msg_ctx *ctx,
  1452. struct net_buf_simple *buf)
  1453. {
  1454. struct bt_mesh_gen_user_prop_srv *srv = model->user_data;
  1455. struct bt_mesh_generic_property *property = NULL;
  1456. u16_t property_id = 0U;
  1457. u8_t expect_len = 0U;
  1458. if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
  1459. BT_ERR("%s, Invalid model user_data", __func__);
  1460. return;
  1461. }
  1462. property_id = net_buf_simple_pull_le16(buf);
  1463. /* Callback the received message to the application layer */
  1464. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1465. bt_mesh_gen_server_recv_set_msg_t set = {
  1466. .user_property_set.id = property_id,
  1467. .user_property_set.value = buf,
  1468. };
  1469. bt_mesh_generic_server_cb_evt_to_btc(
  1470. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  1471. return;
  1472. }
  1473. property = gen_get_user_property(model, property_id);
  1474. if (property == NULL || property->user_access == USER_ACCESS_PROHIBIT ||
  1475. property->user_access == USER_ACCESS_READ) {
  1476. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET) {
  1477. send_gen_user_prop_status(model, ctx, property_id, false);
  1478. }
  1479. send_gen_user_prop_status(model, ctx, property_id, true);
  1480. return;
  1481. }
  1482. /* For BLE Mesh Model BQB test:
  1483. * PTS will send Generic User Property Set/Set Unack messages with
  1484. * invalid device property value length, these messages need to be
  1485. * ignored, otherwise the test case will fail.
  1486. */
  1487. expect_len = bt_mesh_get_dev_prop_len(property_id);
  1488. if (buf->len != expect_len) {
  1489. BT_ERR("%s, Invalid User Property length, ID 0x%04x, expect %d, actual %d",
  1490. __func__, property_id, expect_len, buf->len);
  1491. return;
  1492. }
  1493. net_buf_simple_reset(property->val);
  1494. net_buf_simple_add_mem(property->val, buf->data, MIN(buf->len, property->val->size));
  1495. bt_mesh_gen_server_state_change_t change = {
  1496. .gen_user_prop_set.id = property_id,
  1497. .gen_user_prop_set.value = property->val,
  1498. };
  1499. bt_mesh_generic_server_cb_evt_to_btc(
  1500. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  1501. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET) {
  1502. send_gen_user_prop_status(model, ctx, property_id, false);
  1503. }
  1504. send_gen_user_prop_status(model, ctx, property_id, true);
  1505. return;
  1506. }
  1507. /* Generic Admin Property Server message handlers */
  1508. struct bt_mesh_generic_property *gen_get_admin_property(struct bt_mesh_model *model,
  1509. u16_t property_id)
  1510. {
  1511. struct bt_mesh_gen_admin_prop_srv *srv = model->user_data;
  1512. int i;
  1513. for (i = 0; i < srv->property_count; i++) {
  1514. if (srv->properties[i].id == property_id) {
  1515. return &srv->properties[i];
  1516. }
  1517. }
  1518. return NULL;
  1519. }
  1520. static void send_gen_admin_prop_status(struct bt_mesh_model *model,
  1521. struct bt_mesh_msg_ctx *ctx,
  1522. u16_t property_id, bool publish)
  1523. {
  1524. struct bt_mesh_generic_property *property = NULL;
  1525. struct net_buf_simple *msg = NULL;
  1526. u16_t length = 0U;
  1527. if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) {
  1528. BT_ERR("%s, Invalid User Property ID 0x%04x", __func__, property_id);
  1529. return;
  1530. }
  1531. property = gen_get_admin_property(model, property_id);
  1532. if (property == NULL) {
  1533. BT_WARN("%s, Admin property 0x%04x not exist", __func__, property_id);
  1534. length = sizeof(property_id);
  1535. } else {
  1536. length = sizeof(property->id) + sizeof(property->admin_access) + property->val->len;
  1537. }
  1538. if (publish == false) {
  1539. msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1540. if (msg == NULL) {
  1541. BT_ERR("%s, Failed to allocate memory", __func__);
  1542. return;
  1543. }
  1544. } else {
  1545. msg = bt_mesh_server_get_pub_msg(model, 1 + length);
  1546. if (msg == NULL) {
  1547. return;
  1548. }
  1549. }
  1550. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS);
  1551. if (property == NULL) {
  1552. net_buf_simple_add_le16(msg, property_id);
  1553. } else {
  1554. net_buf_simple_add_le16(msg, property->id);
  1555. net_buf_simple_add_u8(msg, property->admin_access);
  1556. if (ctx->recv_op != BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET ||
  1557. property->admin_access != ADMIN_ACCESS_WRITE) {
  1558. net_buf_simple_add_mem(msg, property->val->data, property->val->len);
  1559. }
  1560. }
  1561. if (publish == false) {
  1562. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1563. bt_mesh_free_buf(msg);
  1564. } else {
  1565. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  1566. }
  1567. return;
  1568. }
  1569. static void gen_admin_prop_get(struct bt_mesh_model *model,
  1570. struct bt_mesh_msg_ctx *ctx,
  1571. struct net_buf_simple *buf)
  1572. {
  1573. struct bt_mesh_gen_admin_prop_srv *srv = model->user_data;
  1574. if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
  1575. BT_ERR("%s, Invalid model user_data", __func__);
  1576. return;
  1577. }
  1578. /* Callback the received message to the application layer */
  1579. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1580. bt_mesh_gen_server_recv_get_msg_t get = {0};
  1581. const u8_t *param = NULL;
  1582. size_t len = 0U;
  1583. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET) {
  1584. get.admin_property_get.id = net_buf_simple_pull_le16(buf);
  1585. param = (const u8_t *)&get;
  1586. len = sizeof(get);
  1587. }
  1588. bt_mesh_generic_server_cb_evt_to_btc(
  1589. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, param, len);
  1590. return;
  1591. }
  1592. switch (ctx->recv_op) {
  1593. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET: {
  1594. struct net_buf_simple *msg = NULL;
  1595. u8_t i = 0U;
  1596. msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1597. if (msg == NULL) {
  1598. BT_ERR("%s, Failed to allocate memory", __func__);
  1599. return;
  1600. }
  1601. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS);
  1602. for (i = 0U; i < srv->property_count; i++) {
  1603. net_buf_simple_add_le16(msg, srv->properties[i].id);
  1604. }
  1605. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1606. bt_mesh_free_buf(msg);
  1607. return;
  1608. }
  1609. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: {
  1610. u16_t property_id = net_buf_simple_pull_le16(buf);
  1611. send_gen_admin_prop_status(model, ctx, property_id, false);
  1612. return;
  1613. }
  1614. default:
  1615. BT_WARN("%s, Unknown Generic Admin Property Get opcode 0x%04x", __func__, ctx->recv_op);
  1616. return;
  1617. }
  1618. }
  1619. static void gen_admin_prop_set(struct bt_mesh_model *model,
  1620. struct bt_mesh_msg_ctx *ctx,
  1621. struct net_buf_simple *buf)
  1622. {
  1623. struct bt_mesh_gen_admin_prop_srv *srv = model->user_data;
  1624. struct bt_mesh_generic_property *property = NULL;
  1625. u16_t property_id = 0U;
  1626. u8_t access = 0U;
  1627. if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
  1628. BT_ERR("%s, Invalid model user_data", __func__);
  1629. return;
  1630. }
  1631. property_id = net_buf_simple_pull_le16(buf);
  1632. access = net_buf_simple_pull_u8(buf);
  1633. if (access > ADMIN_ACCESS_READ_WRITE) {
  1634. BT_ERR("%s, Invalid Admin Access 0x%02x", __func__, access);
  1635. return;
  1636. }
  1637. /* Callback the received message to the application layer */
  1638. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1639. bt_mesh_gen_server_recv_set_msg_t set = {
  1640. .admin_property_set.id = property_id,
  1641. .admin_property_set.access = access,
  1642. .admin_property_set.value = buf,
  1643. };
  1644. bt_mesh_generic_server_cb_evt_to_btc(
  1645. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  1646. return;
  1647. }
  1648. property = gen_get_admin_property(model, property_id);
  1649. if (property == NULL) {
  1650. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET) {
  1651. send_gen_admin_prop_status(model, ctx, property_id, false);
  1652. }
  1653. send_gen_admin_prop_status(model, ctx, property_id, true);
  1654. return;
  1655. }
  1656. property->admin_access = access;
  1657. net_buf_simple_reset(property->val);
  1658. net_buf_simple_add_mem(property->val, buf->data, MIN(buf->len, property->val->size));
  1659. bt_mesh_gen_server_state_change_t change = {
  1660. .gen_admin_prop_set.id = property_id,
  1661. .gen_admin_prop_set.access = property->admin_access,
  1662. .gen_admin_prop_set.value = property->val,
  1663. };
  1664. bt_mesh_generic_server_cb_evt_to_btc(
  1665. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  1666. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET) {
  1667. send_gen_admin_prop_status(model, ctx, property_id, false);
  1668. }
  1669. send_gen_admin_prop_status(model, ctx, property_id, true);
  1670. return;
  1671. }
  1672. /* Generic Manufacturer Property Server message handlers */
  1673. struct bt_mesh_generic_property *gen_get_manu_property(struct bt_mesh_model *model,
  1674. u16_t property_id)
  1675. {
  1676. struct bt_mesh_gen_manu_prop_srv *srv = model->user_data;
  1677. int i;
  1678. for (i = 0; i < srv->property_count; i++) {
  1679. if (srv->properties[i].id == property_id) {
  1680. return &srv->properties[i];
  1681. }
  1682. }
  1683. return NULL;
  1684. }
  1685. static void send_gen_manu_prop_status(struct bt_mesh_model *model,
  1686. struct bt_mesh_msg_ctx *ctx,
  1687. u16_t property_id, bool publish)
  1688. {
  1689. struct bt_mesh_generic_property *property = NULL;
  1690. struct net_buf_simple *msg = NULL;
  1691. u16_t length = 0U;
  1692. if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) {
  1693. BT_ERR("%s, Invalid User Property ID 0x%04x", __func__, property_id);
  1694. return;
  1695. }
  1696. property = gen_get_manu_property(model, property_id);
  1697. if (property == NULL) {
  1698. BT_WARN("%s, Manufacturer property 0x%04x not exist", __func__, property_id);
  1699. length = sizeof(property_id);
  1700. } else {
  1701. length = sizeof(property->id) + sizeof(property->manu_access) + property->val->len;
  1702. }
  1703. if (publish == false) {
  1704. msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1705. if (msg == NULL) {
  1706. BT_ERR("%s, Failed to allocate memory", __func__);
  1707. return;
  1708. }
  1709. } else {
  1710. msg = bt_mesh_server_get_pub_msg(model, 1 + length);
  1711. if (msg == NULL) {
  1712. return;
  1713. }
  1714. }
  1715. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS);
  1716. if (property == NULL) {
  1717. net_buf_simple_add_le16(msg, property_id);
  1718. } else {
  1719. net_buf_simple_add_le16(msg, property->id);
  1720. net_buf_simple_add_u8(msg, property->manu_access);
  1721. net_buf_simple_add_mem(msg, property->val->data, property->val->len);
  1722. }
  1723. if (publish == false) {
  1724. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1725. bt_mesh_free_buf(msg);
  1726. } else {
  1727. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  1728. }
  1729. return;
  1730. }
  1731. static void gen_manu_prop_get(struct bt_mesh_model *model,
  1732. struct bt_mesh_msg_ctx *ctx,
  1733. struct net_buf_simple *buf)
  1734. {
  1735. struct bt_mesh_gen_manu_prop_srv *srv = model->user_data;
  1736. if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
  1737. BT_ERR("%s, Invalid model user_data", __func__);
  1738. return;
  1739. }
  1740. /* Callback the received message to the application layer */
  1741. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1742. bt_mesh_gen_server_recv_get_msg_t get = {0};
  1743. const u8_t *param = NULL;
  1744. size_t len = 0U;
  1745. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET) {
  1746. get.manu_property_get.id = net_buf_simple_pull_le16(buf);
  1747. param = (const u8_t *)&get;
  1748. len = sizeof(get);
  1749. }
  1750. bt_mesh_generic_server_cb_evt_to_btc(
  1751. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, param, len);
  1752. return;
  1753. }
  1754. switch (ctx->recv_op) {
  1755. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET: {
  1756. struct net_buf_simple *msg = NULL;
  1757. u8_t i = 0U;
  1758. msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1759. if (msg == NULL) {
  1760. BT_ERR("%s, Failed to allocate memory", __func__);
  1761. return;
  1762. }
  1763. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS);
  1764. for (i = 0U; i < srv->property_count; i++) {
  1765. net_buf_simple_add_le16(msg, srv->properties[i].id);
  1766. }
  1767. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1768. bt_mesh_free_buf(msg);
  1769. return;
  1770. }
  1771. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET: {
  1772. u16_t property_id = net_buf_simple_pull_le16(buf);
  1773. send_gen_manu_prop_status(model, ctx, property_id, false);
  1774. return;
  1775. }
  1776. default:
  1777. BT_WARN("%s, Unknown Generic Manufacturer Property Get opcode 0x%04x", __func__, ctx->recv_op);
  1778. return;
  1779. }
  1780. }
  1781. static void gen_manu_prop_set(struct bt_mesh_model *model,
  1782. struct bt_mesh_msg_ctx *ctx,
  1783. struct net_buf_simple *buf)
  1784. {
  1785. struct bt_mesh_gen_manu_prop_srv *srv = model->user_data;
  1786. struct bt_mesh_generic_property *property = NULL;
  1787. u16_t property_id = 0U;
  1788. u8_t access = 0U;
  1789. if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
  1790. BT_ERR("%s, Invalid model user_data", __func__);
  1791. return;
  1792. }
  1793. property_id = net_buf_simple_pull_le16(buf);
  1794. access = net_buf_simple_pull_u8(buf);
  1795. if (access > MANU_ACCESS_READ) {
  1796. BT_ERR("%s, Invalid Manufacturer Access 0x%02x", __func__, access);
  1797. return;
  1798. }
  1799. /* Callback the received message to the application layer */
  1800. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1801. bt_mesh_gen_server_recv_set_msg_t set = {
  1802. .manu_property_set.id = property_id,
  1803. .manu_property_set.access = access,
  1804. };
  1805. bt_mesh_generic_server_cb_evt_to_btc(
  1806. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
  1807. return;
  1808. }
  1809. property = gen_get_manu_property(model, property_id);
  1810. if (property == NULL) {
  1811. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET) {
  1812. send_gen_manu_prop_status(model, ctx, property_id, false);
  1813. }
  1814. send_gen_manu_prop_status(model, ctx, property_id, true);
  1815. return;
  1816. }
  1817. property->manu_access = access;
  1818. bt_mesh_gen_server_state_change_t change = {
  1819. .gen_manu_prop_set.id = property_id,
  1820. .gen_manu_prop_set.access = property->manu_access,
  1821. };
  1822. bt_mesh_generic_server_cb_evt_to_btc(
  1823. BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
  1824. if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET) {
  1825. send_gen_manu_prop_status(model, ctx, property_id, false);
  1826. }
  1827. send_gen_manu_prop_status(model, ctx, property_id, true);
  1828. return;
  1829. }
  1830. /* Generic Client Property Server message handlers */
  1831. static int search_prop_id_index(const u16_t *array, u8_t array_idx, u16_t id)
  1832. {
  1833. static const u16_t *start = NULL;
  1834. u8_t index = 0U;
  1835. u16_t temp = 0U;
  1836. if (start == NULL) {
  1837. start = array;
  1838. }
  1839. if (array_idx == 0U) {
  1840. if (*array >= id) {
  1841. return array - start;
  1842. } else {
  1843. return -1;
  1844. }
  1845. }
  1846. index = array_idx / 2;
  1847. temp = array[index];
  1848. if (temp == id) {
  1849. return array + index - start;
  1850. } else if (temp > id) {
  1851. return search_prop_id_index(array, index, id);
  1852. } else {
  1853. return search_prop_id_index(array + index + 1, array_idx - 1 - index, id);
  1854. }
  1855. }
  1856. static void gen_client_prop_get(struct bt_mesh_model *model,
  1857. struct bt_mesh_msg_ctx *ctx,
  1858. struct net_buf_simple *buf)
  1859. {
  1860. struct bt_mesh_gen_client_prop_srv *srv = model->user_data;
  1861. struct net_buf_simple *sdu = NULL;
  1862. u16_t total_len = 5U;
  1863. u16_t property_id = 0U;
  1864. int i, index = 0;
  1865. if (srv == NULL || srv->id_count == 0U || srv->property_ids == NULL) {
  1866. BT_ERR("%s, Invalid model user_data", __func__);
  1867. return;
  1868. }
  1869. /* Callback the received message to the application layer */
  1870. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1871. bt_mesh_gen_server_recv_get_msg_t get = {
  1872. .client_properties_get.id = net_buf_simple_pull_le16(buf),
  1873. };
  1874. bt_mesh_generic_server_cb_evt_to_btc(
  1875. BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get));
  1876. return;
  1877. }
  1878. /* The sequence shall be in an ascending order of Property ID values and shall
  1879. * start with a smallest Property ID that is greater than or equal to the value
  1880. * of the Generic Client Property field of the Generic Client Properities Get
  1881. * message that it is responding to.
  1882. */
  1883. property_id = net_buf_simple_pull_le16(buf);
  1884. index = search_prop_id_index(srv->property_ids, srv->id_count - 1, property_id);
  1885. if (index < 0) {
  1886. NET_BUF_SIMPLE_DEFINE(msg, 1 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1887. bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS);
  1888. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL));
  1889. return;
  1890. }
  1891. sdu = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN));
  1892. if (sdu == NULL) {
  1893. BT_ERR("%s, Failed to allocate memory", __func__);
  1894. return;
  1895. }
  1896. bt_mesh_model_msg_init(sdu, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS);
  1897. for (i = index; i < srv->id_count; i++) {
  1898. total_len += sizeof(u16_t);
  1899. if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
  1900. /* Add this in case the message is too long */
  1901. break;
  1902. }
  1903. net_buf_simple_add_le16(sdu, srv->property_ids[i]);
  1904. }
  1905. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, sdu, NULL, NULL));
  1906. bt_mesh_free_buf(sdu);
  1907. return;
  1908. }
  1909. /* message handlers (End) */
  1910. /* Mapping of message handlers for Generic OnOff Server (0x1000) */
  1911. const struct bt_mesh_model_op gen_onoff_srv_op[] = {
  1912. { BLE_MESH_MODEL_OP_GEN_ONOFF_GET, 0, gen_onoff_get },
  1913. { BLE_MESH_MODEL_OP_GEN_ONOFF_SET, 2, gen_onoff_set },
  1914. { BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK, 2, gen_onoff_set },
  1915. BLE_MESH_MODEL_OP_END,
  1916. };
  1917. /* Mapping of message handlers for Generic Levl Server (0x1002) */
  1918. const struct bt_mesh_model_op gen_level_srv_op[] = {
  1919. { BLE_MESH_MODEL_OP_GEN_LEVEL_GET, 0, gen_level_get },
  1920. { BLE_MESH_MODEL_OP_GEN_LEVEL_SET, 3, gen_level_set },
  1921. { BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK, 3, gen_level_set },
  1922. { BLE_MESH_MODEL_OP_GEN_DELTA_SET, 5, gen_delta_set },
  1923. { BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK, 5, gen_delta_set },
  1924. { BLE_MESH_MODEL_OP_GEN_MOVE_SET, 3, gen_move_set },
  1925. { BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK, 3, gen_move_set },
  1926. BLE_MESH_MODEL_OP_END,
  1927. };
  1928. /* Mapping of message handlers for Generic Default TT Server (0x1004) */
  1929. const struct bt_mesh_model_op gen_def_trans_time_srv_op[] = {
  1930. { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET, 0, gen_def_trans_time_get },
  1931. { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET, 1, gen_def_trans_time_set },
  1932. { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK, 1, gen_def_trans_time_set },
  1933. BLE_MESH_MODEL_OP_END,
  1934. };
  1935. /* Mapping of message handlers for Generic Power OnOff Server (0x1006) */
  1936. const struct bt_mesh_model_op gen_power_onoff_srv_op[] = {
  1937. { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET, 0, gen_onpowerup_get },
  1938. BLE_MESH_MODEL_OP_END,
  1939. };
  1940. /* Mapping of message handlers for Generic Power OnOff Setup Server (0x1007) */
  1941. const struct bt_mesh_model_op gen_power_onoff_setup_srv_op[] = {
  1942. { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET, 1, gen_onpowerup_set },
  1943. { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK, 1, gen_onpowerup_set },
  1944. BLE_MESH_MODEL_OP_END,
  1945. };
  1946. /* Mapping of message handlers for Generic Power Level Server (0x1009) */
  1947. const struct bt_mesh_model_op gen_power_level_srv_op[] = {
  1948. { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET, 0, gen_power_level_get },
  1949. { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET, 3, gen_power_level_set },
  1950. { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK, 3, gen_power_level_set },
  1951. { BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET, 0, gen_power_level_get },
  1952. { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET, 0, gen_power_level_get },
  1953. { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET, 0, gen_power_level_get },
  1954. BLE_MESH_MODEL_OP_END,
  1955. };
  1956. /* Mapping of message handlers for Generic Power Level Setup Server (0x100A) */
  1957. const struct bt_mesh_model_op gen_power_level_setup_srv_op[] = {
  1958. { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET, 2, gen_power_default_set },
  1959. { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK, 2, gen_power_default_set },
  1960. { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET, 4, gen_power_range_set },
  1961. { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK, 4, gen_power_range_set },
  1962. BLE_MESH_MODEL_OP_END,
  1963. };
  1964. /* Mapping of message handlers for Generic Battery Server (0x100C) */
  1965. const struct bt_mesh_model_op gen_battery_srv_op[] = {
  1966. { BLE_MESH_MODEL_OP_GEN_BATTERY_GET, 0, gen_battery_get },
  1967. BLE_MESH_MODEL_OP_END,
  1968. };
  1969. /* Mapping of message handlers for Generic Location Server (0x100E) */
  1970. const struct bt_mesh_model_op gen_location_srv_op[] = {
  1971. { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET, 0, gen_location_get },
  1972. { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET, 0, gen_location_get },
  1973. BLE_MESH_MODEL_OP_END,
  1974. };
  1975. /* Mapping of message handlers for Generic Location Setup Server (0x100F) */
  1976. const struct bt_mesh_model_op gen_location_setup_srv_op[] = {
  1977. { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET, 10, gen_location_set },
  1978. { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK, 10, gen_location_set },
  1979. { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET, 9, gen_location_set },
  1980. { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK, 9, gen_location_set },
  1981. BLE_MESH_MODEL_OP_END,
  1982. };
  1983. /* Mapping of message handlers for Generic User Property Server (0x1013) */
  1984. const struct bt_mesh_model_op gen_user_prop_srv_op[] = {
  1985. { BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET, 0, gen_user_prop_get },
  1986. { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET, 2, gen_user_prop_get },
  1987. { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET, 3, gen_user_prop_set },
  1988. { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK, 3, gen_user_prop_set },
  1989. BLE_MESH_MODEL_OP_END,
  1990. };
  1991. /* Mapping of message handlers for Generic Admin Property Server (0x1011) */
  1992. const struct bt_mesh_model_op gen_admin_prop_srv_op[] = {
  1993. { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET, 0, gen_admin_prop_get },
  1994. { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET, 2, gen_admin_prop_get },
  1995. { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET, 4, gen_admin_prop_set },
  1996. { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK, 4, gen_admin_prop_set },
  1997. BLE_MESH_MODEL_OP_END,
  1998. };
  1999. /* Mapping of message handlers for Generic Manufacturer Property Server (0x1012) */
  2000. const struct bt_mesh_model_op gen_manu_prop_srv_op[] = {
  2001. { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET, 0, gen_manu_prop_get },
  2002. { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET, 2, gen_manu_prop_get },
  2003. { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET, 3, gen_manu_prop_set },
  2004. { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK, 3, gen_manu_prop_set },
  2005. BLE_MESH_MODEL_OP_END,
  2006. };
  2007. /* Mapping of message handlers for Generic Client Property Server (0x1014) */
  2008. const struct bt_mesh_model_op gen_client_prop_srv_op[] = {
  2009. { BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET, 2, gen_client_prop_get },
  2010. BLE_MESH_MODEL_OP_END,
  2011. };
  2012. static inline int property_id_compare(const void *p1, const void *p2)
  2013. {
  2014. if (*(u16_t *)p1 < * (u16_t *)p2) {
  2015. return -1;
  2016. }
  2017. if (*(u16_t *)p1 > *(u16_t *)p2) {
  2018. return 1;
  2019. }
  2020. return 0;
  2021. }
  2022. static int generic_server_init(struct bt_mesh_model *model)
  2023. {
  2024. if (model->user_data == NULL) {
  2025. BT_ERR("%s, No Generic Server context provided, model_id 0x%04x", __func__, model->id);
  2026. return -EINVAL;
  2027. }
  2028. switch (model->id) {
  2029. case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: {
  2030. struct bt_mesh_gen_onoff_srv *srv = model->user_data;
  2031. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2032. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2033. k_delayed_work_init(&srv->transition.timer, generic_onoff_work_handler);
  2034. }
  2035. srv->model = model;
  2036. break;
  2037. }
  2038. case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV: {
  2039. struct bt_mesh_gen_level_srv *srv = model->user_data;
  2040. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2041. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2042. k_delayed_work_init(&srv->transition.timer, generic_level_work_handler);
  2043. }
  2044. srv->model = model;
  2045. break;
  2046. }
  2047. case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV: {
  2048. struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data;
  2049. srv->model = model;
  2050. break;
  2051. }
  2052. case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: {
  2053. struct bt_mesh_gen_power_onoff_srv *srv = model->user_data;
  2054. if (srv->state == NULL) {
  2055. BT_ERR("%s, NULL Generic OnPowerUp State", __func__);
  2056. return -EINVAL;
  2057. }
  2058. srv->model = model;
  2059. break;
  2060. }
  2061. case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: {
  2062. struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data;
  2063. if (srv->state == NULL) {
  2064. BT_ERR("%s, NULL Generic OnPowerUp State", __func__);
  2065. return -EINVAL;
  2066. }
  2067. srv->model = model;
  2068. break;
  2069. }
  2070. case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: {
  2071. struct bt_mesh_gen_power_level_srv *srv = model->user_data;
  2072. if (srv->state == NULL) {
  2073. BT_ERR("%s, NULL Generic Power Level State", __func__);
  2074. return -EINVAL;
  2075. }
  2076. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2077. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2078. k_delayed_work_init(&srv->transition.timer, generic_power_level_work_handler);
  2079. }
  2080. srv->model = model;
  2081. break;
  2082. }
  2083. case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV: {
  2084. struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
  2085. if (srv->state == NULL) {
  2086. BT_ERR("%s, NULL Generic Power Level State", __func__);
  2087. return -EINVAL;
  2088. }
  2089. srv->model = model;
  2090. break;
  2091. }
  2092. case BLE_MESH_MODEL_ID_GEN_BATTERY_SRV: {
  2093. struct bt_mesh_gen_battery_srv *srv = model->user_data;
  2094. srv->model = model;
  2095. break;
  2096. }
  2097. case BLE_MESH_MODEL_ID_GEN_LOCATION_SRV: {
  2098. struct bt_mesh_gen_location_srv *srv = model->user_data;
  2099. if (srv->state == NULL) {
  2100. BT_ERR("%s, NULL Generic Location State", __func__);
  2101. return -EINVAL;
  2102. }
  2103. srv->model = model;
  2104. break;
  2105. }
  2106. case BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV: {
  2107. struct bt_mesh_gen_location_setup_srv *srv = model->user_data;
  2108. if (srv->state == NULL) {
  2109. BT_ERR("%s, NULL Generic Location State", __func__);
  2110. return -EINVAL;
  2111. }
  2112. srv->model = model;
  2113. break;
  2114. }
  2115. case BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV: {
  2116. struct bt_mesh_gen_user_prop_srv *srv = model->user_data;
  2117. if (srv->property_count == 0U || srv->properties == NULL) {
  2118. BT_ERR("%s, NULL Generic User Property State", __func__);
  2119. return -EINVAL;
  2120. }
  2121. srv->model = model;
  2122. break;
  2123. }
  2124. case BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV: {
  2125. struct bt_mesh_gen_admin_prop_srv *srv = model->user_data;
  2126. if (srv->property_count == 0U || srv->properties == NULL) {
  2127. BT_ERR("%s, NULL Generic Admin Property State", __func__);
  2128. return -EINVAL;
  2129. }
  2130. srv->model = model;
  2131. break;
  2132. }
  2133. case BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV: {
  2134. struct bt_mesh_gen_manu_prop_srv *srv = model->user_data;
  2135. if (srv->property_count == 0U || srv->properties == NULL) {
  2136. BT_ERR("%s, NULL Generic Manufacturer Property State", __func__);
  2137. return -EINVAL;
  2138. }
  2139. srv->model = model;
  2140. break;
  2141. }
  2142. case BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV: {
  2143. struct bt_mesh_gen_client_prop_srv *srv = model->user_data;
  2144. if (srv->id_count == 0U || srv->property_ids == NULL) {
  2145. BT_ERR("%s, NULL Generic Client Property State", __func__);
  2146. return -EINVAL;
  2147. }
  2148. /* Quick sort the Client Property IDs in ascending order */
  2149. qsort(srv->property_ids, srv->id_count, sizeof(u16_t), property_id_compare);
  2150. srv->model = model;
  2151. break;
  2152. }
  2153. default:
  2154. BT_WARN("%s, Unknown Generic Server Model, model_id 0x%04x", __func__, model->id);
  2155. return -EINVAL;
  2156. }
  2157. bt_mesh_generic_server_mutex_new();
  2158. return 0;
  2159. }
  2160. int bt_mesh_gen_onoff_srv_init(struct bt_mesh_model *model, bool primary)
  2161. {
  2162. if (model->pub == NULL) {
  2163. BT_ERR("%s, Generic OnOff Server has no publication support", __func__);
  2164. return -EINVAL;
  2165. }
  2166. return generic_server_init(model);
  2167. }
  2168. int bt_mesh_gen_level_srv_init(struct bt_mesh_model *model, bool primary)
  2169. {
  2170. if (model->pub == NULL) {
  2171. BT_ERR("%s, Generic Level Server has no publication support", __func__);
  2172. return -EINVAL;
  2173. }
  2174. return generic_server_init(model);
  2175. }
  2176. int bt_mesh_gen_def_trans_time_srv_init(struct bt_mesh_model *model, bool primary)
  2177. {
  2178. if (model->pub == NULL) {
  2179. BT_ERR("%s, Generic Default Trans Time Server has no publication support", __func__);
  2180. return -EINVAL;
  2181. }
  2182. return generic_server_init(model);
  2183. }
  2184. int bt_mesh_gen_power_onoff_srv_init(struct bt_mesh_model *model, bool primary)
  2185. {
  2186. if (model->pub == NULL) {
  2187. BT_ERR("%s, Generic Power OnOff Server has no publication support", __func__);
  2188. return -EINVAL;
  2189. }
  2190. /* When this model is present on an element, the corresponding Generic
  2191. * Power OnOff Setup Server model shall also be present.
  2192. */
  2193. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  2194. if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV) == NULL) {
  2195. BT_WARN("%s, Generic Power OnOff Setup Server is not present", __func__);
  2196. /* Just give a warning here, continue with the initialization */
  2197. }
  2198. return generic_server_init(model);
  2199. }
  2200. int bt_mesh_gen_power_onoff_setup_srv_init(struct bt_mesh_model *model, bool primary)
  2201. {
  2202. return generic_server_init(model);
  2203. }
  2204. int bt_mesh_gen_power_level_srv_init(struct bt_mesh_model *model, bool primary)
  2205. {
  2206. if (model->pub == NULL) {
  2207. BT_ERR("%s, Generic Power Level Server has no publication support", __func__);
  2208. return -EINVAL;
  2209. }
  2210. /* When this model is present on an Element, the corresponding Generic
  2211. * Power Level Setup Server model shall also be present.
  2212. */
  2213. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  2214. if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV) == NULL) {
  2215. BT_WARN("%s, Generic Power Level Setup Server is not present", __func__);
  2216. /* Just give a warning here, continue with the initialization */
  2217. }
  2218. return generic_server_init(model);
  2219. }
  2220. int bt_mesh_gen_power_level_setup_srv_init(struct bt_mesh_model *model, bool primary)
  2221. {
  2222. return generic_server_init(model);
  2223. }
  2224. int bt_mesh_gen_battery_srv_init(struct bt_mesh_model *model, bool primary)
  2225. {
  2226. if (model->pub == NULL) {
  2227. BT_ERR("%s, Generic Battery Server has no publication support", __func__);
  2228. return -EINVAL;
  2229. }
  2230. return generic_server_init(model);
  2231. }
  2232. int bt_mesh_gen_location_srv_init(struct bt_mesh_model *model, bool primary)
  2233. {
  2234. if (model->pub == NULL) {
  2235. BT_ERR("%s, Generic Location Server has no publication support", __func__);
  2236. return -EINVAL;
  2237. }
  2238. return generic_server_init(model);
  2239. }
  2240. int bt_mesh_gen_location_setup_srv_init(struct bt_mesh_model *model, bool primary)
  2241. {
  2242. /* When this model is present on an Element, the corresponding Generic
  2243. * Location Setup Server model shall also be present.
  2244. */
  2245. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  2246. if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV) == NULL) {
  2247. BT_WARN("%s, Generic Location Setup Server is not present", __func__);
  2248. /* Just give a warning here, continue with the initialization */
  2249. }
  2250. return generic_server_init(model);
  2251. }
  2252. int bt_mesh_gen_user_prop_srv_init(struct bt_mesh_model *model, bool primary)
  2253. {
  2254. if (model->pub == NULL) {
  2255. BT_ERR("%s, Generic User Property has no publication support", __func__);
  2256. return -EINVAL;
  2257. }
  2258. return generic_server_init(model);
  2259. }
  2260. int bt_mesh_gen_admin_prop_srv_init(struct bt_mesh_model *model, bool primary)
  2261. {
  2262. if (model->pub == NULL) {
  2263. BT_ERR("%s, Generic Admin Property has no publication support", __func__);
  2264. return -EINVAL;
  2265. }
  2266. return generic_server_init(model);
  2267. }
  2268. int bt_mesh_gen_manu_prop_srv_init(struct bt_mesh_model *model, bool primary)
  2269. {
  2270. if (model->pub == NULL) {
  2271. BT_ERR("%s, Generic Manufacturer Property has no publication support", __func__);
  2272. return -EINVAL;
  2273. }
  2274. return generic_server_init(model);
  2275. }
  2276. int bt_mesh_gen_client_prop_srv_init(struct bt_mesh_model *model, bool primary)
  2277. {
  2278. if (model->pub == NULL) {
  2279. BT_ERR("%s, Generic Client Property has no publication support", __func__);
  2280. return -EINVAL;
  2281. }
  2282. return generic_server_init(model);
  2283. }
  2284. static int generic_server_deinit(struct bt_mesh_model *model)
  2285. {
  2286. if (model->user_data == NULL) {
  2287. BT_ERR("%s, No Generic Server context provided, model_id 0x%04x", __func__, model->id);
  2288. return -EINVAL;
  2289. }
  2290. switch (model->id) {
  2291. case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: {
  2292. struct bt_mesh_gen_onoff_srv *srv = model->user_data;
  2293. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2294. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  2295. k_delayed_work_free(&srv->transition.timer);
  2296. }
  2297. break;
  2298. }
  2299. case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV: {
  2300. struct bt_mesh_gen_level_srv *srv = model->user_data;
  2301. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2302. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  2303. k_delayed_work_free(&srv->transition.timer);
  2304. }
  2305. break;
  2306. }
  2307. case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: {
  2308. struct bt_mesh_gen_power_level_srv *srv = model->user_data;
  2309. if (srv->state == NULL) {
  2310. BT_ERR("%s, NULL Generic Power Level State", __func__);
  2311. return -EINVAL;
  2312. }
  2313. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2314. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  2315. k_delayed_work_free(&srv->transition.timer);
  2316. }
  2317. break;
  2318. }
  2319. default:
  2320. BT_WARN("%s, Unknown Generic Server Model, model_id 0x%04x", __func__, model->id);
  2321. return -EINVAL;
  2322. }
  2323. bt_mesh_generic_server_mutex_free();
  2324. return 0;
  2325. }
  2326. int bt_mesh_gen_onoff_srv_deinit(struct bt_mesh_model *model, bool primary)
  2327. {
  2328. if (model->pub == NULL) {
  2329. BT_ERR("%s, Generic OnOff Server has no publication support", __func__);
  2330. return -EINVAL;
  2331. }
  2332. return generic_server_deinit(model);
  2333. }
  2334. int bt_mesh_gen_level_srv_deinit(struct bt_mesh_model *model, bool primary)
  2335. {
  2336. if (model->pub == NULL) {
  2337. BT_ERR("%s, Generic Level Server has no publication support", __func__);
  2338. return -EINVAL;
  2339. }
  2340. return generic_server_deinit(model);
  2341. }
  2342. int bt_mesh_gen_def_trans_time_srv_deinit(struct bt_mesh_model *model, bool primary)
  2343. {
  2344. if (model->pub == NULL) {
  2345. BT_ERR("%s, Generic Default Trans Time Server has no publication support", __func__);
  2346. return -EINVAL;
  2347. }
  2348. return generic_server_deinit(model);
  2349. }
  2350. int bt_mesh_gen_power_onoff_srv_deinit(struct bt_mesh_model *model, bool primary)
  2351. {
  2352. if (model->pub == NULL) {
  2353. BT_ERR("%s, Generic Power OnOff Server has no publication support", __func__);
  2354. return -EINVAL;
  2355. }
  2356. return generic_server_deinit(model);
  2357. }
  2358. int bt_mesh_gen_power_onoff_setup_srv_deinit(struct bt_mesh_model *model, bool primary)
  2359. {
  2360. return generic_server_deinit(model);
  2361. }
  2362. int bt_mesh_gen_power_level_srv_deinit(struct bt_mesh_model *model, bool primary)
  2363. {
  2364. if (model->pub == NULL) {
  2365. BT_ERR("%s, Generic Power Level Server has no publication support", __func__);
  2366. return -EINVAL;
  2367. }
  2368. return generic_server_deinit(model);
  2369. }
  2370. int bt_mesh_gen_power_level_setup_srv_deinit(struct bt_mesh_model *model, bool primary)
  2371. {
  2372. return generic_server_deinit(model);
  2373. }
  2374. int bt_mesh_gen_battery_srv_deinit(struct bt_mesh_model *model, bool primary)
  2375. {
  2376. if (model->pub == NULL) {
  2377. BT_ERR("%s, Generic Battery Server has no publication support", __func__);
  2378. return -EINVAL;
  2379. }
  2380. return generic_server_deinit(model);
  2381. }
  2382. int bt_mesh_gen_location_srv_deinit(struct bt_mesh_model *model, bool primary)
  2383. {
  2384. if (model->pub == NULL) {
  2385. BT_ERR("%s, Generic Location Server has no publication support", __func__);
  2386. return -EINVAL;
  2387. }
  2388. return generic_server_deinit(model);
  2389. }
  2390. int bt_mesh_gen_location_setup_srv_deinit(struct bt_mesh_model *model, bool primary)
  2391. {
  2392. return generic_server_deinit(model);
  2393. }
  2394. int bt_mesh_gen_user_prop_srv_deinit(struct bt_mesh_model *model, bool primary)
  2395. {
  2396. if (model->pub == NULL) {
  2397. BT_ERR("%s, Generic User Property has no publication support", __func__);
  2398. return -EINVAL;
  2399. }
  2400. return generic_server_deinit(model);
  2401. }
  2402. int bt_mesh_gen_admin_prop_srv_deinit(struct bt_mesh_model *model, bool primary)
  2403. {
  2404. if (model->pub == NULL) {
  2405. BT_ERR("%s, Generic Admin Property has no publication support", __func__);
  2406. return -EINVAL;
  2407. }
  2408. return generic_server_deinit(model);
  2409. }
  2410. int bt_mesh_gen_manu_prop_srv_deinit(struct bt_mesh_model *model, bool primary)
  2411. {
  2412. if (model->pub == NULL) {
  2413. BT_ERR("%s, Generic Manufacturer Property has no publication support", __func__);
  2414. return -EINVAL;
  2415. }
  2416. return generic_server_deinit(model);
  2417. }
  2418. int bt_mesh_gen_client_prop_srv_deinit(struct bt_mesh_model *model, bool primary)
  2419. {
  2420. if (model->pub == NULL) {
  2421. BT_ERR("%s, Generic Client Property has no publication support", __func__);
  2422. return -EINVAL;
  2423. }
  2424. return generic_server_deinit(model);
  2425. }