generic_server.c 99 KB

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