generic_server.c 99 KB

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