lighting_server.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659
  1. /* Bluetooth: Mesh Lighting 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_lighting_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_LIGHTING_SERVER
  17. static bt_mesh_mutex_t light_server_lock;
  18. static inline void bt_mesh_light_server_mutex_new(void)
  19. {
  20. if (!light_server_lock.mutex) {
  21. bt_mesh_mutex_create(&light_server_lock);
  22. }
  23. }
  24. #if CONFIG_BLE_MESH_DEINIT
  25. static inline void bt_mesh_light_server_mutex_free(void)
  26. {
  27. bt_mesh_mutex_free(&light_server_lock);
  28. }
  29. #endif /* CONFIG_BLE_MESH_DEINIT */
  30. void bt_mesh_light_server_lock(void)
  31. {
  32. bt_mesh_mutex_lock(&light_server_lock);
  33. }
  34. void bt_mesh_light_server_unlock(void)
  35. {
  36. bt_mesh_mutex_unlock(&light_server_lock);
  37. }
  38. /* message handlers (Start) */
  39. /* Light Lightness Server/Setup Server message handlers */
  40. static void send_light_lightness_status(struct bt_mesh_model *model,
  41. struct bt_mesh_msg_ctx *ctx,
  42. bool publish, uint16_t opcode)
  43. {
  44. struct net_buf_simple *msg = NULL;
  45. uint8_t length = 2 + 5;
  46. if (ctx == NULL && publish == false) {
  47. BT_ERR("%s, Invalid parameter", __func__);
  48. return;
  49. }
  50. if (publish == false) {
  51. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  52. if (msg == NULL) {
  53. BT_ERR("%s, Out of memory", __func__);
  54. return;
  55. }
  56. } else {
  57. msg = bt_mesh_server_get_pub_msg(model, length);
  58. if (msg == NULL) {
  59. return;
  60. }
  61. }
  62. bt_mesh_model_msg_init(msg, opcode);
  63. switch (opcode) {
  64. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS: {
  65. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  66. net_buf_simple_add_le16(msg, srv->state->lightness_actual);
  67. if (srv->actual_transition.counter) {
  68. bt_mesh_server_calc_remain_time(&srv->actual_transition);
  69. net_buf_simple_add_le16(msg, srv->state->target_lightness_actual);
  70. net_buf_simple_add_u8(msg, srv->actual_transition.remain_time);
  71. }
  72. break;
  73. }
  74. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS: {
  75. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  76. net_buf_simple_add_le16(msg, srv->state->lightness_linear);
  77. if (srv->linear_transition.counter) {
  78. bt_mesh_server_calc_remain_time(&srv->linear_transition);
  79. net_buf_simple_add_le16(msg, srv->state->target_lightness_linear);
  80. net_buf_simple_add_u8(msg, srv->linear_transition.remain_time);
  81. }
  82. break;
  83. }
  84. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS: {
  85. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  86. net_buf_simple_add_le16(msg, srv->state->lightness_last);
  87. break;
  88. }
  89. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS:
  90. if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) {
  91. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  92. net_buf_simple_add_le16(msg, srv->state->lightness_default);
  93. } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) {
  94. struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
  95. net_buf_simple_add_le16(msg, srv->state->lightness_default);
  96. }
  97. break;
  98. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS:
  99. if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) {
  100. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  101. net_buf_simple_add_u8(msg, srv->state->status_code);
  102. net_buf_simple_add_le16(msg, srv->state->lightness_range_min);
  103. net_buf_simple_add_le16(msg, srv->state->lightness_range_max);
  104. } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) {
  105. struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
  106. net_buf_simple_add_u8(msg, srv->state->status_code);
  107. net_buf_simple_add_le16(msg, srv->state->lightness_range_min);
  108. net_buf_simple_add_le16(msg, srv->state->lightness_range_max);
  109. }
  110. break;
  111. default:
  112. BT_WARN("Unknown Light Lightness status opcode 0x%04x", opcode);
  113. if (publish == false) {
  114. bt_mesh_free_buf(msg);
  115. }
  116. return;
  117. }
  118. if (publish == false) {
  119. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  120. bt_mesh_free_buf(msg);
  121. } else {
  122. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  123. }
  124. return;
  125. }
  126. static void light_lightness_get(struct bt_mesh_model *model,
  127. struct bt_mesh_msg_ctx *ctx,
  128. struct net_buf_simple *buf)
  129. {
  130. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  131. uint16_t opcode = 0U;
  132. if (srv == NULL || srv->state == NULL) {
  133. BT_ERR("%s, Invalid model user data", __func__);
  134. return;
  135. }
  136. /* Callback the received message to the application layer */
  137. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  138. bt_mesh_lighting_server_cb_evt_to_btc(
  139. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  140. return;
  141. }
  142. switch (ctx->recv_op) {
  143. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET:
  144. opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS;
  145. break;
  146. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET:
  147. opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS;
  148. break;
  149. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET:
  150. opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS;
  151. break;
  152. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET:
  153. opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS;
  154. break;
  155. case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET:
  156. opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS;
  157. break;
  158. default:
  159. BT_WARN("Unknown Light Lightness Get opcode 0x%04x", ctx->recv_op);
  160. return;
  161. }
  162. send_light_lightness_status(model, ctx, false, opcode);
  163. return;
  164. }
  165. void light_lightness_publish(struct bt_mesh_model *model, uint16_t opcode)
  166. {
  167. if (model->user_data == NULL) {
  168. BT_ERR("%s, Invalid model user data", __func__);
  169. return;
  170. }
  171. switch (model->id) {
  172. case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: {
  173. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  174. if (srv->state == NULL) {
  175. BT_ERR("Invalid Light Lightness Server state");
  176. return;
  177. }
  178. break;
  179. }
  180. case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV: {
  181. struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
  182. if (srv->state == NULL) {
  183. BT_ERR("Invalid Light Lightness Setup Server state");
  184. return;
  185. }
  186. break;
  187. }
  188. default:
  189. BT_ERR("Invalid Light Lightness Server model 0x%04x", model->id);
  190. return;
  191. }
  192. send_light_lightness_status(model, NULL, true, opcode);
  193. return;
  194. }
  195. static void light_lightness_set(struct bt_mesh_model *model,
  196. struct bt_mesh_msg_ctx *ctx,
  197. struct net_buf_simple *buf)
  198. {
  199. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  200. uint8_t tid = 0U, trans_time = 0U, delay = 0U;
  201. bool optional = false;
  202. uint16_t actual = 0U;
  203. int64_t now = 0;
  204. if (srv == NULL || srv->state == NULL) {
  205. BT_ERR("%s, Invalid model user data", __func__);
  206. return;
  207. }
  208. actual = net_buf_simple_pull_le16(buf);
  209. tid = net_buf_simple_pull_u8(buf);
  210. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  211. return;
  212. }
  213. /* Callback the received message to the application layer */
  214. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  215. bt_mesh_light_server_recv_set_msg_t set = {
  216. .lightness_set.op_en = optional,
  217. .lightness_set.lightness = actual,
  218. .lightness_set.tid = tid,
  219. .lightness_set.trans_time = trans_time,
  220. .lightness_set.delay = delay,
  221. };
  222. bt_mesh_lighting_server_cb_evt_to_btc(
  223. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  224. return;
  225. }
  226. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  227. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
  228. send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
  229. }
  230. send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
  231. /* In this condition, no event will be callback to application layer */
  232. return;
  233. }
  234. bt_mesh_light_server_lock();
  235. bt_mesh_server_stop_transition(&srv->actual_transition);
  236. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  237. if (actual) {
  238. if (srv->state->lightness_range_min && actual < srv->state->lightness_range_min) {
  239. actual = srv->state->lightness_range_min;
  240. } else if (srv->state->lightness_range_max && actual > srv->state->lightness_range_max) {
  241. actual = srv->state->lightness_range_max;
  242. }
  243. }
  244. srv->state->target_lightness_actual = actual;
  245. /**
  246. * If the target state is equal to the current state, the transition shall not be
  247. * started and is considered complete.
  248. */
  249. if (srv->state->target_lightness_actual != srv->state->lightness_actual) {
  250. light_lightness_actual_tt_values(srv, trans_time, delay);
  251. } else {
  252. bt_mesh_light_server_state_change_t change = {
  253. .lightness_set.lightness = srv->state->lightness_actual,
  254. };
  255. bt_mesh_lighting_server_cb_evt_to_btc(
  256. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  257. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
  258. send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
  259. }
  260. send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
  261. bt_mesh_light_server_unlock();
  262. return;
  263. }
  264. /* Copy the ctx of the received message */
  265. if (srv->actual_transition.timer.work._reserved) {
  266. memcpy(srv->actual_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  267. }
  268. /* For Instantaneous Transition */
  269. if (srv->actual_transition.counter == 0U) {
  270. srv->state->lightness_actual = srv->state->target_lightness_actual;
  271. /**
  272. * Whenever the Light Lightness Actual state is changed with a non-transactional
  273. * message or a completed sequence of transactional messages to a non-zero value,
  274. * the value of the Light Lightness Last shall be set to the value of the Light
  275. * Lightness Actual.
  276. */
  277. if (srv->state->lightness_actual) {
  278. srv->state->lightness_last = srv->state->lightness_actual;
  279. }
  280. }
  281. srv->actual_transition.just_started = true;
  282. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
  283. send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
  284. }
  285. send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
  286. bt_mesh_light_server_unlock();
  287. bt_mesh_server_start_transition(&srv->actual_transition);
  288. return;
  289. }
  290. static void light_lightness_linear_set(struct bt_mesh_model *model,
  291. struct bt_mesh_msg_ctx *ctx,
  292. struct net_buf_simple *buf)
  293. {
  294. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  295. uint8_t tid = 0U, trans_time = 0U, delay = 0U;
  296. bool optional = false;
  297. uint16_t linear = 0U;
  298. int64_t now = 0;
  299. if (srv == NULL || srv->state == NULL) {
  300. BT_ERR("%s, Invalid model user data", __func__);
  301. return;
  302. }
  303. linear = net_buf_simple_pull_le16(buf);
  304. tid = net_buf_simple_pull_u8(buf);
  305. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  306. return;
  307. }
  308. /* Callback the received message to the application layer */
  309. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  310. bt_mesh_light_server_recv_set_msg_t set = {
  311. .lightness_linear_set.op_en = optional,
  312. .lightness_linear_set.lightness = linear,
  313. .lightness_linear_set.tid = tid,
  314. .lightness_linear_set.trans_time = trans_time,
  315. .lightness_linear_set.delay = delay,
  316. };
  317. bt_mesh_lighting_server_cb_evt_to_btc(
  318. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  319. return;
  320. }
  321. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  322. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
  323. send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
  324. }
  325. send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
  326. /* In this condition, no event will be callback to application layer */
  327. return;
  328. }
  329. bt_mesh_light_server_lock();
  330. bt_mesh_server_stop_transition(&srv->linear_transition);
  331. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  332. srv->state->target_lightness_linear = linear;
  333. /**
  334. * If the target state is equal to the current state, the transition shall not
  335. * be started and is considered complete.
  336. */
  337. if (srv->state->target_lightness_linear != srv->state->lightness_linear) {
  338. light_lightness_linear_tt_values(srv, trans_time, delay);
  339. } else {
  340. bt_mesh_light_server_state_change_t change = {
  341. .lightness_linear_set.lightness = srv->state->lightness_actual,
  342. };
  343. bt_mesh_lighting_server_cb_evt_to_btc(
  344. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  345. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
  346. send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
  347. }
  348. send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
  349. bt_mesh_light_server_unlock();
  350. return;
  351. }
  352. /* Copy the ctx of the received message */
  353. if (srv->linear_transition.timer.work._reserved) {
  354. memcpy(srv->linear_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  355. }
  356. /* For Instantaneous Transition */
  357. if (srv->linear_transition.counter == 0U) {
  358. srv->state->lightness_linear = srv->state->target_lightness_linear;
  359. }
  360. srv->linear_transition.just_started = true;
  361. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
  362. send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
  363. }
  364. send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
  365. bt_mesh_light_server_unlock();
  366. bt_mesh_server_start_transition(&srv->linear_transition);
  367. return;
  368. }
  369. static void light_lightness_default_set(struct bt_mesh_model *model,
  370. struct bt_mesh_msg_ctx *ctx,
  371. struct net_buf_simple *buf)
  372. {
  373. struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
  374. uint16_t lightness = 0U;
  375. if (srv == NULL || srv->state == NULL) {
  376. BT_ERR("%s, Invalid model user data", __func__);
  377. return;
  378. }
  379. lightness = net_buf_simple_pull_le16(buf);
  380. /* Callback the received message to the application layer */
  381. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  382. bt_mesh_light_server_recv_set_msg_t set = {
  383. .lightness_default_set.lightness = lightness,
  384. };
  385. bt_mesh_lighting_server_cb_evt_to_btc(
  386. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  387. return;
  388. }
  389. if (srv->state->lightness_default != lightness) {
  390. srv->state->lightness_default = lightness;
  391. bt_mesh_light_server_state_change_t change = {
  392. .lightness_default_set.lightness = lightness,
  393. };
  394. bt_mesh_lighting_server_cb_evt_to_btc(
  395. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  396. }
  397. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET) {
  398. send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS);
  399. }
  400. send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS);
  401. return;
  402. }
  403. static void light_lightness_range_set(struct bt_mesh_model *model,
  404. struct bt_mesh_msg_ctx *ctx,
  405. struct net_buf_simple *buf)
  406. {
  407. struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
  408. uint16_t range_min = 0U, range_max = 0U;
  409. if (srv == NULL || srv->state == NULL) {
  410. BT_ERR("%s, Invalid model user data", __func__);
  411. return;
  412. }
  413. range_min = net_buf_simple_pull_le16(buf);
  414. range_max = net_buf_simple_pull_le16(buf);
  415. if (range_min > range_max) {
  416. BT_ERR("Range min 0x%04x is greater than range max 0x%04x",
  417. range_min, range_max);
  418. return;
  419. }
  420. /* Callback the received message to the application layer */
  421. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  422. bt_mesh_light_server_recv_set_msg_t set = {
  423. .lightness_range_set.range_min = range_min,
  424. .lightness_range_set.range_max = range_max,
  425. };
  426. bt_mesh_lighting_server_cb_evt_to_btc(
  427. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  428. return;
  429. }
  430. /**
  431. * When a Light Lightness Setup Server receives a Light Lightness Range Set
  432. * message or a Light Lightness Range Set Unacknowledged message with values
  433. * that cannot be accepted, it shall set the status of the operation to a
  434. * value representing the reason why the values cannot be accepted.
  435. *
  436. * TODO: 0x0000 for Light Range Min/Max is prohibited, but BQB test case
  437. * MMDL/SR/LLNS/BI-01-C requires 'SUCCESS' when it sends a set message with
  438. * Light Range Min set to 0x0000.
  439. */
  440. #if 0
  441. srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
  442. #else
  443. if (range_min == 0x0000) {
  444. srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN;
  445. } else if (range_max == 0x0000) {
  446. srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX;
  447. } else {
  448. srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
  449. }
  450. #endif
  451. if (range_min && srv->state->lightness_range_min != range_min) {
  452. srv->state->lightness_range_min = range_min;
  453. }
  454. if (range_max && srv->state->lightness_range_max != range_max) {
  455. srv->state->lightness_range_max = range_max;
  456. }
  457. bt_mesh_light_server_state_change_t change = {
  458. .lightness_range_set.range_min = srv->state->lightness_range_min,
  459. .lightness_range_set.range_max = srv->state->lightness_range_max,
  460. };
  461. bt_mesh_lighting_server_cb_evt_to_btc(
  462. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  463. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET) {
  464. send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS);
  465. }
  466. send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS);
  467. return;
  468. }
  469. /* Light CTL Server/Temperature Server/Setup Server message handlers */
  470. static void send_light_ctl_status(struct bt_mesh_model *model,
  471. struct bt_mesh_msg_ctx *ctx,
  472. bool publish, uint16_t opcode)
  473. {
  474. struct net_buf_simple *msg = NULL;
  475. uint8_t length = 2 + 9;
  476. if (ctx == NULL && publish == false) {
  477. BT_ERR("%s, Invalid parameter", __func__);
  478. return;
  479. }
  480. if (publish == false) {
  481. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  482. if (msg == NULL) {
  483. BT_ERR("%s, Out of memory", __func__);
  484. return;
  485. }
  486. } else {
  487. msg = bt_mesh_server_get_pub_msg(model, length);
  488. if (msg == NULL) {
  489. return;
  490. }
  491. }
  492. bt_mesh_model_msg_init(msg, opcode);
  493. switch (opcode) {
  494. case BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS: {
  495. struct bt_mesh_light_ctl_srv *srv = model->user_data;
  496. net_buf_simple_add_le16(msg, srv->state->lightness);
  497. net_buf_simple_add_le16(msg, srv->state->temperature);
  498. if (srv->transition.counter) {
  499. bt_mesh_server_calc_remain_time(&srv->transition);
  500. net_buf_simple_add_le16(msg, srv->state->target_lightness);
  501. net_buf_simple_add_le16(msg, srv->state->target_temperature);
  502. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  503. }
  504. break;
  505. }
  506. case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS:
  507. if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SRV) {
  508. struct bt_mesh_light_ctl_srv *srv = model->user_data;
  509. net_buf_simple_add_u8(msg, srv->state->status_code);
  510. net_buf_simple_add_le16(msg, srv->state->temperature_range_min);
  511. net_buf_simple_add_le16(msg, srv->state->temperature_range_max);
  512. } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) {
  513. struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
  514. net_buf_simple_add_u8(msg, srv->state->status_code);
  515. net_buf_simple_add_le16(msg, srv->state->temperature_range_min);
  516. net_buf_simple_add_le16(msg, srv->state->temperature_range_max);
  517. }
  518. break;
  519. case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS: {
  520. if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SRV) {
  521. struct bt_mesh_light_ctl_srv *srv = model->user_data;
  522. net_buf_simple_add_le16(msg, srv->state->lightness_default);
  523. net_buf_simple_add_le16(msg, srv->state->temperature_default);
  524. net_buf_simple_add_le16(msg, srv->state->delta_uv_default);
  525. } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) {
  526. struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
  527. net_buf_simple_add_le16(msg, srv->state->lightness_default);
  528. net_buf_simple_add_le16(msg, srv->state->temperature_default);
  529. net_buf_simple_add_le16(msg, srv->state->delta_uv_default);
  530. }
  531. break;
  532. }
  533. case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS: {
  534. struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
  535. net_buf_simple_add_le16(msg, srv->state->temperature);
  536. net_buf_simple_add_le16(msg, srv->state->delta_uv);
  537. if (srv->transition.counter) {
  538. bt_mesh_server_calc_remain_time(&srv->transition);
  539. net_buf_simple_add_le16(msg, srv->state->target_temperature);
  540. net_buf_simple_add_le16(msg, srv->state->target_delta_uv);
  541. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  542. }
  543. break;
  544. }
  545. default:
  546. BT_WARN("Unknown Light CTL status opcode 0x%04x", opcode);
  547. if (publish == false) {
  548. bt_mesh_free_buf(msg);
  549. }
  550. return;
  551. }
  552. if (publish == false) {
  553. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  554. bt_mesh_free_buf(msg);
  555. } else {
  556. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  557. }
  558. return;
  559. }
  560. static void light_ctl_get(struct bt_mesh_model *model,
  561. struct bt_mesh_msg_ctx *ctx,
  562. struct net_buf_simple *buf)
  563. {
  564. struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL;
  565. uint16_t opcode = 0U;
  566. if (model->user_data == NULL) {
  567. BT_ERR("%s, Invalid model user data", __func__);
  568. return;
  569. }
  570. switch (model->id) {
  571. case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
  572. struct bt_mesh_light_ctl_srv *srv = model->user_data;
  573. if (srv->state == NULL) {
  574. BT_ERR("Invalid Light CTL Server state");
  575. return;
  576. }
  577. rsp_ctrl = &srv->rsp_ctrl;
  578. break;
  579. }
  580. case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
  581. struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
  582. if (srv->state == NULL) {
  583. BT_ERR("Invalid Light CTL Temperature Server state");
  584. return;
  585. }
  586. rsp_ctrl = &srv->rsp_ctrl;
  587. break;
  588. }
  589. default:
  590. BT_ERR("Invalid Light CTL Server model 0x%04x", model->id);
  591. return;
  592. }
  593. /* Callback the received message to the application layer */
  594. if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  595. bt_mesh_lighting_server_cb_evt_to_btc(
  596. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  597. return;
  598. }
  599. switch (ctx->recv_op) {
  600. case BLE_MESH_MODEL_OP_LIGHT_CTL_GET:
  601. opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS;
  602. break;
  603. case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET:
  604. opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS;
  605. break;
  606. case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET:
  607. opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS;
  608. break;
  609. case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET:
  610. opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS;
  611. break;
  612. default:
  613. BT_WARN("Unknown Light CTL Get opcode 0x%04x", ctx->recv_op);
  614. return;
  615. }
  616. send_light_ctl_status(model, ctx, false, opcode);
  617. return;
  618. }
  619. void light_ctl_publish(struct bt_mesh_model *model, uint16_t opcode)
  620. {
  621. if (model->user_data == NULL) {
  622. BT_ERR("%s, Invalid model user data", __func__);
  623. return;
  624. }
  625. switch (model->id) {
  626. case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
  627. struct bt_mesh_light_ctl_srv *srv = model->user_data;
  628. if (srv->state == NULL) {
  629. BT_ERR("Invalid Light CTL Server state");
  630. return;
  631. }
  632. break;
  633. }
  634. case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
  635. struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
  636. if (srv->state == NULL) {
  637. BT_ERR("Invalid Light CTL Temperature Server state");
  638. return;
  639. }
  640. break;
  641. }
  642. case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV: {
  643. struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
  644. if (srv->state == NULL) {
  645. BT_ERR("Invalid Light CTL Setup Server state");
  646. return;
  647. }
  648. break;
  649. }
  650. default:
  651. BT_ERR("Invalid Light CTL Server model 0x%04x", model->id);
  652. return;
  653. }
  654. send_light_ctl_status(model, NULL, true, opcode);
  655. return;
  656. }
  657. static void light_ctl_set(struct bt_mesh_model *model,
  658. struct bt_mesh_msg_ctx *ctx,
  659. struct net_buf_simple *buf)
  660. {
  661. struct bt_mesh_light_ctl_srv *srv = model->user_data;
  662. uint16_t lightness = 0U, temperature = 0U;
  663. uint8_t tid = 0U, trans_time = 0U, delay = 0U;
  664. int16_t delta_uv = 0;
  665. bool optional = false;
  666. int64_t now = 0;
  667. if (srv == NULL || srv->state == NULL) {
  668. BT_ERR("%s, Invalid model user data", __func__);
  669. return;
  670. }
  671. lightness = net_buf_simple_pull_le16(buf);
  672. temperature = net_buf_simple_pull_le16(buf);
  673. delta_uv = (int16_t) net_buf_simple_pull_le16(buf);
  674. tid = net_buf_simple_pull_u8(buf);
  675. if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) {
  676. BT_ERR("Invalid temperature 0x%04x", temperature);
  677. return;
  678. }
  679. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  680. return;
  681. }
  682. /* Callback the received message to the application layer */
  683. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  684. bt_mesh_light_server_recv_set_msg_t set = {
  685. .ctl_set.op_en = optional,
  686. .ctl_set.lightness = lightness,
  687. .ctl_set.temperature = temperature,
  688. .ctl_set.delta_uv = delta_uv,
  689. .ctl_set.tid = tid,
  690. .ctl_set.trans_time = trans_time,
  691. .ctl_set.delay = delay,
  692. };
  693. bt_mesh_lighting_server_cb_evt_to_btc(
  694. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  695. return;
  696. }
  697. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  698. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
  699. send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
  700. }
  701. send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
  702. /* In this condition, no event will be callback to application layer */
  703. return;
  704. }
  705. bt_mesh_light_server_lock();
  706. bt_mesh_server_stop_transition(&srv->transition);
  707. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  708. srv->state->target_lightness = lightness;
  709. if (srv->state->temperature_range_min &&
  710. srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN &&
  711. temperature < srv->state->temperature_range_min) {
  712. temperature = srv->state->temperature_range_min;
  713. } else if (srv->state->temperature_range_max &&
  714. srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN &&
  715. temperature > srv->state->temperature_range_max) {
  716. temperature = srv->state->temperature_range_max;
  717. }
  718. srv->state->target_temperature = temperature;
  719. srv->state->target_delta_uv = delta_uv;
  720. if (srv->state->target_lightness != srv->state->lightness ||
  721. srv->state->target_temperature != srv->state->temperature ||
  722. srv->state->target_delta_uv != srv->state->delta_uv) {
  723. light_ctl_tt_values(srv, trans_time, delay);
  724. } else {
  725. bt_mesh_light_server_state_change_t change = {
  726. .ctl_set.lightness = srv->state->lightness,
  727. .ctl_set.temperature = srv->state->temperature,
  728. .ctl_set.delta_uv = srv->state->delta_uv,
  729. };
  730. bt_mesh_lighting_server_cb_evt_to_btc(
  731. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  732. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
  733. send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
  734. }
  735. send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
  736. bt_mesh_light_server_unlock();
  737. return;
  738. }
  739. /* Copy the ctx of the received message */
  740. if (srv->transition.timer.work._reserved) {
  741. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  742. }
  743. /* For Instantaneous Transition */
  744. if (srv->transition.counter == 0U) {
  745. srv->state->lightness = srv->state->target_lightness;
  746. srv->state->temperature = srv->state->target_temperature;
  747. srv->state->delta_uv = srv->state->target_delta_uv;
  748. }
  749. srv->transition.just_started = true;
  750. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
  751. send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
  752. }
  753. send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
  754. bt_mesh_light_server_unlock();
  755. bt_mesh_server_start_transition(&srv->transition);
  756. return;
  757. }
  758. static void light_ctl_default_set(struct bt_mesh_model *model,
  759. struct bt_mesh_msg_ctx *ctx,
  760. struct net_buf_simple *buf)
  761. {
  762. struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
  763. uint16_t lightness = 0U, temperature = 0U;
  764. int16_t delta_uv = 0;
  765. if (srv == NULL || srv->state == NULL) {
  766. BT_ERR("%s, Invalid model user data", __func__);
  767. return;
  768. }
  769. lightness = net_buf_simple_pull_le16(buf);
  770. temperature = net_buf_simple_pull_le16(buf);
  771. delta_uv = (int16_t) net_buf_simple_pull_le16(buf);
  772. if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) {
  773. BT_ERR("Invalid temperature 0x%04x", temperature);
  774. return;
  775. }
  776. /* Callback the received message to the application layer */
  777. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  778. bt_mesh_light_server_recv_set_msg_t set = {
  779. .ctl_default_set.lightness = lightness,
  780. .ctl_default_set.temperature = temperature,
  781. .ctl_default_set.delta_uv = delta_uv,
  782. };
  783. bt_mesh_lighting_server_cb_evt_to_btc(
  784. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  785. return;
  786. }
  787. if (srv->state->temperature_range_min &&
  788. srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN &&
  789. temperature < srv->state->temperature_range_min) {
  790. temperature = srv->state->temperature_range_min;
  791. } else if (srv->state->temperature_range_max &&
  792. srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN &&
  793. temperature > srv->state->temperature_range_max) {
  794. temperature = srv->state->temperature_range_max;
  795. }
  796. srv->state->lightness_default = lightness;
  797. srv->state->temperature_default = temperature;
  798. srv->state->delta_uv_default = delta_uv;
  799. bt_mesh_light_server_state_change_t change = {
  800. .ctl_default_set.lightness = srv->state->lightness_default,
  801. .ctl_default_set.temperature = srv->state->temperature_default,
  802. .ctl_default_set.delta_uv = srv->state->delta_uv_default,
  803. };
  804. bt_mesh_lighting_server_cb_evt_to_btc(
  805. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  806. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET) {
  807. send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS);
  808. }
  809. send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS);
  810. return;
  811. }
  812. static void light_ctl_temp_range_set(struct bt_mesh_model *model,
  813. struct bt_mesh_msg_ctx *ctx,
  814. struct net_buf_simple *buf)
  815. {
  816. struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
  817. uint16_t min = 0U, max = 0U;
  818. if (srv == NULL || srv->state == NULL) {
  819. BT_ERR("%s, Invalid model user data", __func__);
  820. return;
  821. }
  822. min = net_buf_simple_pull_le16(buf);
  823. max = net_buf_simple_pull_le16(buf);
  824. /* This is as per 6.1.3.1 in Mesh Model Specification */
  825. if (min > max ||
  826. min < BLE_MESH_TEMPERATURE_MIN || (min != BLE_MESH_TEMPERATURE_UNKNOWN && min > BLE_MESH_TEMPERATURE_MAX) ||
  827. max < BLE_MESH_TEMPERATURE_MIN || (max != BLE_MESH_TEMPERATURE_UNKNOWN && max > BLE_MESH_TEMPERATURE_MAX)) {
  828. BT_ERR("Invalid parameter, range min 0x%04x, range max 0x%04x",
  829. min, max);
  830. return;
  831. }
  832. /* Callback the received message to the application layer */
  833. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  834. bt_mesh_light_server_recv_set_msg_t set = {
  835. .ctl_temp_range_set.range_min = min,
  836. .ctl_temp_range_set.range_max = max,
  837. };
  838. bt_mesh_lighting_server_cb_evt_to_btc(
  839. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  840. return;
  841. }
  842. if (min == BLE_MESH_TEMPERATURE_UNKNOWN) {
  843. srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN;
  844. } else if (max == BLE_MESH_TEMPERATURE_UNKNOWN ) {
  845. srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX;
  846. } else {
  847. srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
  848. }
  849. if (min != BLE_MESH_TEMPERATURE_UNKNOWN && srv->state->temperature_range_min != min) {
  850. srv->state->temperature_range_min = min;
  851. }
  852. if (max != BLE_MESH_TEMPERATURE_UNKNOWN && srv->state->temperature_range_max != max) {
  853. srv->state->temperature_range_max = max;
  854. }
  855. bt_mesh_light_server_state_change_t change = {
  856. .ctl_temp_range_set.range_min = srv->state->temperature_range_min,
  857. .ctl_temp_range_set.range_max = srv->state->temperature_range_max,
  858. };
  859. bt_mesh_lighting_server_cb_evt_to_btc(
  860. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  861. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET) {
  862. send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS);
  863. }
  864. send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS);
  865. return;
  866. }
  867. static void light_ctl_temp_set(struct bt_mesh_model *model,
  868. struct bt_mesh_msg_ctx *ctx,
  869. struct net_buf_simple *buf)
  870. {
  871. struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
  872. uint8_t tid = 0U, trans_time = 0U, delay = 0U;
  873. uint16_t temperature = 0U;
  874. int16_t delta_uv = 0;
  875. bool optional = false;
  876. int64_t now = 0;
  877. if (srv == NULL || srv->state == NULL) {
  878. BT_ERR("%s, Invalid model user data", __func__);
  879. return;
  880. }
  881. temperature = net_buf_simple_pull_le16(buf);
  882. delta_uv = (int16_t) net_buf_simple_pull_le16(buf);
  883. tid = net_buf_simple_pull_u8(buf);
  884. if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) {
  885. BT_ERR("Invalid temperature 0x%04x", temperature);
  886. return;
  887. }
  888. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  889. return;
  890. }
  891. /* Callback the received message to the application layer */
  892. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  893. bt_mesh_light_server_recv_set_msg_t set = {
  894. .ctl_temp_set.op_en = optional,
  895. .ctl_temp_set.temperature = temperature,
  896. .ctl_temp_set.delta_uv = delta_uv,
  897. .ctl_temp_set.tid = tid,
  898. .ctl_temp_set.trans_time = trans_time,
  899. .ctl_temp_set.delay = delay,
  900. };
  901. bt_mesh_lighting_server_cb_evt_to_btc(
  902. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  903. return;
  904. }
  905. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  906. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) {
  907. send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
  908. }
  909. send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
  910. /* In this condition, no event will be callback to application layer */
  911. return;
  912. }
  913. bt_mesh_light_server_lock();
  914. bt_mesh_server_stop_transition(&srv->transition);
  915. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  916. if (srv->state->temperature_range_min &&
  917. srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN &&
  918. temperature < srv->state->temperature_range_min) {
  919. temperature = srv->state->temperature_range_min;
  920. } else if (srv->state->temperature_range_max &&
  921. srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN &&
  922. temperature > srv->state->temperature_range_max) {
  923. temperature = srv->state->temperature_range_max;
  924. }
  925. srv->state->target_temperature = temperature;
  926. srv->state->target_delta_uv = delta_uv;
  927. if (srv->state->target_temperature != srv->state->temperature ||
  928. srv->state->target_delta_uv != srv->state->delta_uv) {
  929. light_ctl_temp_tt_values(srv, trans_time, delay);
  930. } else {
  931. bt_mesh_light_server_state_change_t change = {
  932. .ctl_temp_set.temperature = srv->state->temperature,
  933. .ctl_temp_set.delta_uv = srv->state->delta_uv,
  934. };
  935. bt_mesh_lighting_server_cb_evt_to_btc(
  936. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  937. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) {
  938. send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
  939. }
  940. send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
  941. bt_mesh_light_server_unlock();
  942. return;
  943. }
  944. /* Copy the ctx of the received message */
  945. if (srv->transition.timer.work._reserved) {
  946. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  947. }
  948. /* For Instantaneous Transition */
  949. if (srv->transition.counter == 0U) {
  950. srv->state->temperature = srv->state->target_temperature;
  951. srv->state->delta_uv = srv->state->target_delta_uv;
  952. }
  953. srv->transition.just_started = true;
  954. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) {
  955. send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
  956. }
  957. send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
  958. bt_mesh_light_server_unlock();
  959. bt_mesh_server_start_transition(&srv->transition);
  960. return;
  961. }
  962. /* Light HSL Server/Hue Server/Saturation Server/Setup Server message handlers */
  963. static void send_light_hsl_status(struct bt_mesh_model *model,
  964. struct bt_mesh_msg_ctx *ctx,
  965. bool publish, uint16_t opcode)
  966. {
  967. struct net_buf_simple *msg = NULL;
  968. uint8_t length = 2 + 9;
  969. if (ctx == NULL && publish == false) {
  970. BT_ERR("%s, Invalid parameter", __func__);
  971. return;
  972. }
  973. if (publish == false) {
  974. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  975. if (msg == NULL) {
  976. BT_ERR("%s, Out of memory", __func__);
  977. return;
  978. }
  979. } else {
  980. msg = bt_mesh_server_get_pub_msg(model, length);
  981. if (msg == NULL) {
  982. return;
  983. }
  984. }
  985. bt_mesh_model_msg_init(msg, opcode);
  986. switch (opcode) {
  987. case BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS:
  988. case BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS: {
  989. struct bt_mesh_light_hsl_srv *srv = model->user_data;
  990. if (opcode == BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS) {
  991. net_buf_simple_add_le16(msg, srv->state->lightness);
  992. net_buf_simple_add_le16(msg, srv->state->hue);
  993. net_buf_simple_add_le16(msg, srv->state->saturation);
  994. if (srv->transition.counter) {
  995. bt_mesh_server_calc_remain_time(&srv->transition);
  996. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  997. }
  998. } else if (opcode == BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS) {
  999. net_buf_simple_add_le16(msg, srv->state->target_lightness);
  1000. net_buf_simple_add_le16(msg, srv->state->target_hue);
  1001. net_buf_simple_add_le16(msg, srv->state->target_saturation);
  1002. if (srv->transition.counter) {
  1003. bt_mesh_server_calc_remain_time(&srv->transition);
  1004. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  1005. }
  1006. }
  1007. break;
  1008. }
  1009. case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS:
  1010. if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SRV) {
  1011. struct bt_mesh_light_hsl_srv *srv = model->user_data;
  1012. net_buf_simple_add_le16(msg, srv->state->lightness_default);
  1013. net_buf_simple_add_le16(msg, srv->state->hue_default);
  1014. net_buf_simple_add_le16(msg, srv->state->saturation_default);
  1015. } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) {
  1016. struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
  1017. net_buf_simple_add_le16(msg, srv->state->lightness_default);
  1018. net_buf_simple_add_le16(msg, srv->state->hue_default);
  1019. net_buf_simple_add_le16(msg, srv->state->saturation_default);
  1020. }
  1021. break;
  1022. case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS:
  1023. if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SRV) {
  1024. struct bt_mesh_light_hsl_srv *srv = model->user_data;
  1025. net_buf_simple_add_u8(msg, srv->state->status_code);
  1026. net_buf_simple_add_le16(msg, srv->state->hue_range_min);
  1027. net_buf_simple_add_le16(msg, srv->state->hue_range_max);
  1028. net_buf_simple_add_le16(msg, srv->state->saturation_range_min);
  1029. net_buf_simple_add_le16(msg, srv->state->saturation_range_max);
  1030. } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) {
  1031. struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
  1032. net_buf_simple_add_u8(msg, srv->state->status_code);
  1033. net_buf_simple_add_le16(msg, srv->state->hue_range_min);
  1034. net_buf_simple_add_le16(msg, srv->state->hue_range_max);
  1035. net_buf_simple_add_le16(msg, srv->state->saturation_range_min);
  1036. net_buf_simple_add_le16(msg, srv->state->saturation_range_max);
  1037. }
  1038. break;
  1039. case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS: {
  1040. struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
  1041. net_buf_simple_add_le16(msg, srv->state->hue);
  1042. if (srv->transition.counter) {
  1043. bt_mesh_server_calc_remain_time(&srv->transition);
  1044. net_buf_simple_add_le16(msg, srv->state->target_hue);
  1045. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  1046. }
  1047. break;
  1048. }
  1049. case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS: {
  1050. struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
  1051. net_buf_simple_add_le16(msg, srv->state->saturation);
  1052. if (srv->transition.counter) {
  1053. bt_mesh_server_calc_remain_time(&srv->transition);
  1054. net_buf_simple_add_le16(msg, srv->state->target_saturation);
  1055. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  1056. }
  1057. break;
  1058. }
  1059. default:
  1060. BT_WARN("Unknown Light HSL status opcode 0x%04x", opcode);
  1061. if (publish == false) {
  1062. bt_mesh_free_buf(msg);
  1063. }
  1064. return;
  1065. }
  1066. if (publish == false) {
  1067. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1068. bt_mesh_free_buf(msg);
  1069. } else {
  1070. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  1071. }
  1072. return;
  1073. }
  1074. static void light_hsl_get(struct bt_mesh_model *model,
  1075. struct bt_mesh_msg_ctx *ctx,
  1076. struct net_buf_simple *buf)
  1077. {
  1078. struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL;
  1079. uint16_t opcode = 0U;
  1080. if (model->user_data == NULL) {
  1081. BT_ERR("%s, Invalid model user data", __func__);
  1082. return;
  1083. }
  1084. switch (model->id) {
  1085. case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: {
  1086. struct bt_mesh_light_hsl_srv *srv = model->user_data;
  1087. if (srv->state == NULL) {
  1088. BT_ERR("Invalid Light HSL Server state");
  1089. return;
  1090. }
  1091. rsp_ctrl = &srv->rsp_ctrl;
  1092. break;
  1093. }
  1094. case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: {
  1095. struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
  1096. if (srv->state == NULL) {
  1097. BT_ERR("Invalid Light HSL Hue Server state");
  1098. return;
  1099. }
  1100. rsp_ctrl = &srv->rsp_ctrl;
  1101. break;
  1102. }
  1103. case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: {
  1104. struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
  1105. if (srv->state == NULL) {
  1106. BT_ERR("Invalid Light HSL Saturation Server state");
  1107. return;
  1108. }
  1109. rsp_ctrl = &srv->rsp_ctrl;
  1110. break;
  1111. }
  1112. default:
  1113. BT_ERR("Invalid Light HSL Server model 0x%04x", model->id);
  1114. return;
  1115. }
  1116. /* Callback the received message to the application layer */
  1117. if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1118. bt_mesh_lighting_server_cb_evt_to_btc(
  1119. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  1120. return;
  1121. }
  1122. switch (ctx->recv_op) {
  1123. case BLE_MESH_MODEL_OP_LIGHT_HSL_GET:
  1124. opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS;
  1125. break;
  1126. case BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET:
  1127. opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS;
  1128. break;
  1129. case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET:
  1130. opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS;
  1131. break;
  1132. case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET:
  1133. opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS;
  1134. break;
  1135. case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET:
  1136. opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS;
  1137. break;
  1138. case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET:
  1139. opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS;
  1140. break;
  1141. default:
  1142. BT_WARN("Unknown Light HSL Get opcode 0x%04x", ctx->recv_op);
  1143. return;
  1144. }
  1145. send_light_hsl_status(model, ctx, false, opcode);
  1146. return;
  1147. }
  1148. void light_hsl_publish(struct bt_mesh_model *model, uint16_t opcode)
  1149. {
  1150. if (model->user_data == NULL) {
  1151. BT_ERR("%s, Invalid model user data", __func__);
  1152. return;
  1153. }
  1154. switch (model->id) {
  1155. case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: {
  1156. struct bt_mesh_light_hsl_srv *srv = model->user_data;
  1157. if (srv->state == NULL) {
  1158. BT_ERR("Invalid Light HSL Server state");
  1159. return;
  1160. }
  1161. break;
  1162. }
  1163. case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: {
  1164. struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
  1165. if (srv->state == NULL) {
  1166. BT_ERR("Invalid Light HSL Hue Server state");
  1167. return;
  1168. }
  1169. break;
  1170. }
  1171. case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: {
  1172. struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
  1173. if (srv->state == NULL) {
  1174. BT_ERR("Invalid Light HSL Saturation Server state");
  1175. return;
  1176. }
  1177. break;
  1178. }
  1179. case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV: {
  1180. struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
  1181. if (srv->state == NULL) {
  1182. BT_ERR("Invalid Light HSL Setup Server state");
  1183. return;
  1184. }
  1185. break;
  1186. }
  1187. default:
  1188. BT_ERR("Invalid Light HSL Server model 0x%04x", model->id);
  1189. return;
  1190. }
  1191. send_light_hsl_status(model, NULL, true, opcode);
  1192. return;
  1193. }
  1194. static void light_hsl_set(struct bt_mesh_model *model,
  1195. struct bt_mesh_msg_ctx *ctx,
  1196. struct net_buf_simple *buf)
  1197. {
  1198. struct bt_mesh_light_hsl_srv *srv = model->user_data;
  1199. uint16_t lightness = 0U, hue = 0U, saturation = 0U;
  1200. uint8_t tid = 0U, trans_time = 0U, delay = 0U;
  1201. bool optional = false;
  1202. int64_t now = 0;
  1203. if (srv == NULL || srv->state == NULL) {
  1204. BT_ERR("%s, Invalid model user data", __func__);
  1205. return;
  1206. }
  1207. lightness = net_buf_simple_pull_le16(buf);
  1208. hue = net_buf_simple_pull_le16(buf);
  1209. saturation = net_buf_simple_pull_le16(buf);
  1210. tid = net_buf_simple_pull_u8(buf);
  1211. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  1212. return;
  1213. }
  1214. /* Callback the received message to the application layer */
  1215. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1216. bt_mesh_light_server_recv_set_msg_t set = {
  1217. .hsl_set.op_en = optional,
  1218. .hsl_set.lightness = lightness,
  1219. .hsl_set.hue = hue,
  1220. .hsl_set.saturation = saturation,
  1221. .hsl_set.tid = tid,
  1222. .hsl_set.trans_time = trans_time,
  1223. .hsl_set.delay = delay,
  1224. };
  1225. bt_mesh_lighting_server_cb_evt_to_btc(
  1226. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  1227. return;
  1228. }
  1229. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  1230. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) {
  1231. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
  1232. }
  1233. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
  1234. /* In this condition, no event will be callback to application layer */
  1235. return;
  1236. }
  1237. bt_mesh_light_server_lock();
  1238. bt_mesh_server_stop_transition(&srv->transition);
  1239. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  1240. srv->state->target_lightness = lightness;
  1241. if (srv->state->hue_range_min && hue < srv->state->hue_range_min) {
  1242. hue = srv->state->hue_range_min;
  1243. } else if (srv->state->hue_range_max && hue > srv->state->hue_range_max) {
  1244. hue = srv->state->hue_range_max;
  1245. }
  1246. srv->state->target_hue = hue;
  1247. if (srv->state->saturation_range_min && saturation < srv->state->saturation_range_min) {
  1248. saturation = srv->state->saturation_range_min;
  1249. } else if (srv->state->saturation_range_max && saturation > srv->state->saturation_range_max) {
  1250. saturation = srv->state->saturation_range_max;
  1251. }
  1252. srv->state->target_saturation = saturation;
  1253. /**
  1254. * If the target state is equal to the current state, the transition shall not
  1255. * be started and is considered complete.
  1256. */
  1257. if (srv->state->target_lightness != srv->state->lightness ||
  1258. srv->state->target_hue != srv->state->hue ||
  1259. srv->state->target_saturation != srv->state->saturation) {
  1260. light_hsl_tt_values(srv, trans_time, delay);
  1261. } else {
  1262. bt_mesh_light_server_state_change_t change = {
  1263. .hsl_set.lightness = srv->state->lightness,
  1264. .hsl_set.hue = srv->state->hue,
  1265. .hsl_set.saturation = srv->state->saturation,
  1266. };
  1267. bt_mesh_lighting_server_cb_evt_to_btc(
  1268. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  1269. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) {
  1270. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
  1271. }
  1272. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
  1273. bt_mesh_light_server_unlock();
  1274. return;
  1275. }
  1276. /* Copy the ctx of the received message */
  1277. if (srv->transition.timer.work._reserved) {
  1278. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  1279. }
  1280. /* For Instantaneous Transition */
  1281. if (srv->transition.counter == 0U) {
  1282. srv->state->lightness = srv->state->target_lightness;
  1283. srv->state->hue = srv->state->target_hue;
  1284. srv->state->saturation = srv->state->target_saturation;
  1285. }
  1286. srv->transition.just_started = true;
  1287. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) {
  1288. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
  1289. }
  1290. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
  1291. bt_mesh_light_server_unlock();
  1292. bt_mesh_server_start_transition(&srv->transition);
  1293. return;
  1294. }
  1295. static void light_hsl_default_set(struct bt_mesh_model *model,
  1296. struct bt_mesh_msg_ctx *ctx,
  1297. struct net_buf_simple *buf)
  1298. {
  1299. struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
  1300. uint16_t lightness = 0U, hue = 0U, saturation = 0U;
  1301. if (srv == NULL || srv->state == NULL) {
  1302. BT_ERR("%s, Invalid model user data", __func__);
  1303. return;
  1304. }
  1305. lightness = net_buf_simple_pull_le16(buf);
  1306. hue = net_buf_simple_pull_le16(buf);
  1307. saturation = net_buf_simple_pull_le16(buf);
  1308. /* Callback the received message to the application layer */
  1309. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1310. bt_mesh_light_server_recv_set_msg_t set = {
  1311. .hsl_default_set.lightness = lightness,
  1312. .hsl_default_set.hue = hue,
  1313. .hsl_default_set.saturation = saturation,
  1314. };
  1315. bt_mesh_lighting_server_cb_evt_to_btc(
  1316. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  1317. return;
  1318. }
  1319. if (srv->state->hue_range_min && hue < srv->state->hue_range_min) {
  1320. hue = srv->state->hue_range_min;
  1321. } else if (srv->state->hue_range_max && hue > srv->state->hue_range_max) {
  1322. hue = srv->state->hue_range_max;
  1323. }
  1324. if (srv->state->saturation_range_min && saturation < srv->state->saturation_range_min) {
  1325. saturation = srv->state->saturation_range_min;
  1326. } else if (srv->state->saturation_range_max && saturation > srv->state->saturation_range_max) {
  1327. saturation = srv->state->saturation_range_max;
  1328. }
  1329. srv->state->lightness_default = lightness;
  1330. srv->state->hue_default = hue;
  1331. srv->state->saturation_default = saturation;
  1332. bt_mesh_light_server_state_change_t change = {
  1333. .hsl_default_set.lightness = srv->state->lightness_default,
  1334. .hsl_default_set.hue = srv->state->hue_default,
  1335. .hsl_default_set.saturation = srv->state->saturation_default,
  1336. };
  1337. bt_mesh_lighting_server_cb_evt_to_btc(
  1338. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  1339. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET) {
  1340. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS);
  1341. }
  1342. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS);
  1343. return;
  1344. }
  1345. static void light_hsl_range_set(struct bt_mesh_model *model,
  1346. struct bt_mesh_msg_ctx *ctx,
  1347. struct net_buf_simple *buf)
  1348. {
  1349. struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
  1350. uint16_t hue_min = 0U, hue_max = 0U, saturation_min = 0U, saturation_max = 0U;
  1351. if (srv == NULL || srv->state == NULL) {
  1352. BT_ERR("%s, Invalid model user data", __func__);
  1353. return;
  1354. }
  1355. hue_min = net_buf_simple_pull_le16(buf);
  1356. hue_max = net_buf_simple_pull_le16(buf);
  1357. saturation_min = net_buf_simple_pull_le16(buf);
  1358. saturation_max = net_buf_simple_pull_le16(buf);
  1359. if (hue_min > hue_max) {
  1360. BT_ERR("Invalid parameter, hue min 0x%04x, hue max 0x%04x",
  1361. hue_min, hue_max);
  1362. return;
  1363. }
  1364. if (saturation_min > saturation_max) {
  1365. BT_ERR("Invalid parameter, saturation min 0x%04x, saturation max 0x%04x",
  1366. saturation_min, saturation_max);
  1367. return;
  1368. }
  1369. /* Callback the received message to the application layer */
  1370. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1371. bt_mesh_light_server_recv_set_msg_t set = {
  1372. .hsl_range_set.hue_range_min = hue_min,
  1373. .hsl_range_set.hue_range_max = hue_max,
  1374. .hsl_range_set.sat_range_min = saturation_min,
  1375. .hsl_range_set.sat_range_max = saturation_max,
  1376. };
  1377. bt_mesh_lighting_server_cb_evt_to_btc(
  1378. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  1379. return;
  1380. }
  1381. srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
  1382. srv->state->hue_range_min = hue_min;
  1383. srv->state->hue_range_max = hue_max;
  1384. srv->state->saturation_range_min = saturation_min;
  1385. srv->state->saturation_range_max = saturation_max;
  1386. bt_mesh_light_server_state_change_t change = {
  1387. .hsl_range_set.hue_range_min = srv->state->hue_range_min,
  1388. .hsl_range_set.hue_range_max = srv->state->hue_range_max,
  1389. .hsl_range_set.sat_range_min = srv->state->saturation_range_min,
  1390. .hsl_range_set.sat_range_max = srv->state->saturation_range_max,
  1391. };
  1392. bt_mesh_lighting_server_cb_evt_to_btc(
  1393. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  1394. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET) {
  1395. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS);
  1396. }
  1397. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS);
  1398. return;
  1399. }
  1400. static void light_hsl_hue_set(struct bt_mesh_model *model,
  1401. struct bt_mesh_msg_ctx *ctx,
  1402. struct net_buf_simple *buf)
  1403. {
  1404. struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
  1405. uint8_t tid = 0U, trans_time = 0U, delay = 0U;
  1406. bool optional = false;
  1407. uint16_t hue = 0U;
  1408. int64_t now = 0;
  1409. if (srv == NULL || srv->state == NULL) {
  1410. BT_ERR("%s, Invalid model user data", __func__);
  1411. return;
  1412. }
  1413. hue = net_buf_simple_pull_le16(buf);
  1414. tid = net_buf_simple_pull_u8(buf);
  1415. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  1416. return;
  1417. }
  1418. /* Callback the received message to the application layer */
  1419. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1420. bt_mesh_light_server_recv_set_msg_t set = {
  1421. .hsl_hue_set.op_en = optional,
  1422. .hsl_hue_set.hue = hue,
  1423. .hsl_hue_set.tid = tid,
  1424. .hsl_hue_set.trans_time = trans_time,
  1425. .hsl_hue_set.delay = delay,
  1426. };
  1427. bt_mesh_lighting_server_cb_evt_to_btc(
  1428. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  1429. return;
  1430. }
  1431. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  1432. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) {
  1433. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
  1434. }
  1435. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
  1436. /* In this condition, no event will be callback to application layer */
  1437. return;
  1438. }
  1439. bt_mesh_light_server_lock();
  1440. bt_mesh_server_stop_transition(&srv->transition);
  1441. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  1442. if (srv->state->hue_range_min && hue < srv->state->hue_range_min) {
  1443. hue = srv->state->hue_range_min;
  1444. } else if (srv->state->hue_range_max && hue > srv->state->hue_range_max) {
  1445. hue = srv->state->hue_range_max;
  1446. }
  1447. srv->state->target_hue = hue;
  1448. /**
  1449. * If the target state is equal to the current state, the transition shall not
  1450. * be started and is considered complete.
  1451. */
  1452. if (srv->state->target_hue != srv->state->hue) {
  1453. light_hsl_hue_tt_values(srv, trans_time, delay);
  1454. } else {
  1455. bt_mesh_light_server_state_change_t change = {
  1456. .hsl_hue_set.hue = srv->state->hue,
  1457. };
  1458. bt_mesh_lighting_server_cb_evt_to_btc(
  1459. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  1460. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) {
  1461. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
  1462. }
  1463. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
  1464. bt_mesh_light_server_unlock();
  1465. return;
  1466. }
  1467. /* Copy the ctx of the received message */
  1468. if (srv->transition.timer.work._reserved) {
  1469. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  1470. }
  1471. /* For Instantaneous Transition */
  1472. if (srv->transition.counter == 0U) {
  1473. srv->state->hue = srv->state->target_hue;
  1474. }
  1475. srv->transition.just_started = true;
  1476. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) {
  1477. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
  1478. }
  1479. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
  1480. bt_mesh_light_server_unlock();
  1481. bt_mesh_server_start_transition(&srv->transition);
  1482. return;
  1483. }
  1484. static void light_hsl_sat_set(struct bt_mesh_model *model,
  1485. struct bt_mesh_msg_ctx *ctx,
  1486. struct net_buf_simple *buf)
  1487. {
  1488. struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
  1489. uint8_t tid = 0U, trans_time = 0U, delay = 0U;
  1490. uint16_t saturation = 0U;
  1491. bool optional = false;
  1492. int64_t now = 0;
  1493. if (srv == NULL || srv->state == NULL) {
  1494. BT_ERR("%s, Invalid model user data", __func__);
  1495. return;
  1496. }
  1497. saturation = net_buf_simple_pull_le16(buf);
  1498. tid = net_buf_simple_pull_u8(buf);
  1499. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  1500. return;
  1501. }
  1502. /* Callback the received message to the application layer */
  1503. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1504. bt_mesh_light_server_recv_set_msg_t set = {
  1505. .hsl_saturation_set.op_en = optional,
  1506. .hsl_saturation_set.saturation = saturation,
  1507. .hsl_saturation_set.tid = tid,
  1508. .hsl_saturation_set.trans_time = trans_time,
  1509. .hsl_saturation_set.delay = delay,
  1510. };
  1511. bt_mesh_lighting_server_cb_evt_to_btc(
  1512. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  1513. return;
  1514. }
  1515. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  1516. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) {
  1517. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
  1518. }
  1519. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
  1520. /* In this condition, no event will be callback to application layer */
  1521. return;
  1522. }
  1523. bt_mesh_light_server_lock();
  1524. bt_mesh_server_stop_transition(&srv->transition);
  1525. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  1526. if (srv->state->saturation_range_min && saturation < srv->state->saturation_range_min) {
  1527. saturation = srv->state->saturation_range_min;
  1528. } else if (srv->state->saturation_range_max && saturation > srv->state->saturation_range_max) {
  1529. saturation = srv->state->saturation_range_max;
  1530. }
  1531. srv->state->target_saturation = saturation;
  1532. /**
  1533. * If the target state is equal to the current state, the transition shall not
  1534. * be started and is considered complete.
  1535. */
  1536. if (srv->state->target_saturation != srv->state->saturation) {
  1537. light_hsl_sat_tt_values(srv, trans_time, delay);
  1538. } else {
  1539. bt_mesh_light_server_state_change_t change = {
  1540. .hsl_saturation_set.saturation = srv->state->saturation,
  1541. };
  1542. bt_mesh_lighting_server_cb_evt_to_btc(
  1543. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  1544. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) {
  1545. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
  1546. }
  1547. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
  1548. bt_mesh_light_server_unlock();
  1549. return;
  1550. }
  1551. /* Copy the ctx of the received message */
  1552. if (srv->transition.timer.work._reserved) {
  1553. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  1554. }
  1555. /* For Instantaneous Transition */
  1556. if (srv->transition.counter == 0U) {
  1557. srv->state->saturation = srv->state->target_saturation;
  1558. }
  1559. srv->transition.just_started = true;
  1560. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) {
  1561. send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
  1562. }
  1563. send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
  1564. bt_mesh_light_server_unlock();
  1565. bt_mesh_server_start_transition(&srv->transition);
  1566. return;
  1567. }
  1568. /* Light xyL Server/Setup Server message handlers */
  1569. static void send_light_xyl_status(struct bt_mesh_model *model,
  1570. struct bt_mesh_msg_ctx *ctx,
  1571. bool publish, uint16_t opcode)
  1572. {
  1573. struct net_buf_simple *msg = NULL;
  1574. uint8_t length = 2 + 9;
  1575. if (ctx == NULL && publish == false) {
  1576. BT_ERR("%s, Invalid parameter", __func__);
  1577. return;
  1578. }
  1579. if (publish == false) {
  1580. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1581. if (msg == NULL) {
  1582. BT_ERR("%s, Out of memory", __func__);
  1583. return;
  1584. }
  1585. } else {
  1586. msg = bt_mesh_server_get_pub_msg(model, length);
  1587. if (msg == NULL) {
  1588. return;
  1589. }
  1590. }
  1591. bt_mesh_model_msg_init(msg, opcode);
  1592. switch (opcode) {
  1593. case BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS:
  1594. case BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS: {
  1595. struct bt_mesh_light_xyl_srv *srv = model->user_data;
  1596. if (opcode == BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS) {
  1597. net_buf_simple_add_le16(msg, srv->state->lightness);
  1598. net_buf_simple_add_le16(msg, srv->state->x);
  1599. net_buf_simple_add_le16(msg, srv->state->y);
  1600. if (srv->transition.counter) {
  1601. bt_mesh_server_calc_remain_time(&srv->transition);
  1602. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  1603. }
  1604. } else if (opcode == BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS) {
  1605. net_buf_simple_add_le16(msg, srv->state->target_lightness);
  1606. net_buf_simple_add_le16(msg, srv->state->target_x);
  1607. net_buf_simple_add_le16(msg, srv->state->target_y);
  1608. if (srv->transition.counter) {
  1609. bt_mesh_server_calc_remain_time(&srv->transition);
  1610. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  1611. }
  1612. }
  1613. break;
  1614. }
  1615. case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS:
  1616. if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SRV) {
  1617. struct bt_mesh_light_xyl_srv *srv = model->user_data;
  1618. net_buf_simple_add_le16(msg, srv->state->lightness_default);
  1619. net_buf_simple_add_le16(msg, srv->state->x_default);
  1620. net_buf_simple_add_le16(msg, srv->state->y_default);
  1621. } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV) {
  1622. struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
  1623. net_buf_simple_add_le16(msg, srv->state->lightness_default);
  1624. net_buf_simple_add_le16(msg, srv->state->x_default);
  1625. net_buf_simple_add_le16(msg, srv->state->y_default);
  1626. }
  1627. break;
  1628. case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS:
  1629. if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SRV) {
  1630. struct bt_mesh_light_xyl_srv *srv = model->user_data;
  1631. net_buf_simple_add_u8(msg, srv->state->status_code);
  1632. net_buf_simple_add_le16(msg, srv->state->x_range_min);
  1633. net_buf_simple_add_le16(msg, srv->state->x_range_max);
  1634. net_buf_simple_add_le16(msg, srv->state->y_range_min);
  1635. net_buf_simple_add_le16(msg, srv->state->y_range_max);
  1636. } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV) {
  1637. struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
  1638. net_buf_simple_add_u8(msg, srv->state->status_code);
  1639. net_buf_simple_add_le16(msg, srv->state->x_range_min);
  1640. net_buf_simple_add_le16(msg, srv->state->x_range_max);
  1641. net_buf_simple_add_le16(msg, srv->state->y_range_min);
  1642. net_buf_simple_add_le16(msg, srv->state->y_range_max);
  1643. }
  1644. break;
  1645. default:
  1646. BT_WARN("Unknown Light xyL status opcode 0x%04x", opcode);
  1647. if (publish == false) {
  1648. bt_mesh_free_buf(msg);
  1649. }
  1650. return;
  1651. }
  1652. if (publish == false) {
  1653. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1654. bt_mesh_free_buf(msg);
  1655. } else {
  1656. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  1657. }
  1658. return;
  1659. }
  1660. static void light_xyl_get(struct bt_mesh_model *model,
  1661. struct bt_mesh_msg_ctx *ctx,
  1662. struct net_buf_simple *buf)
  1663. {
  1664. struct bt_mesh_light_xyl_srv *srv = model->user_data;
  1665. uint16_t opcode = 0U;
  1666. if (srv == NULL || srv->state == NULL) {
  1667. BT_ERR("%s, Invalid model user data", __func__);
  1668. return;
  1669. }
  1670. /* Callback the received message to the application layer */
  1671. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1672. bt_mesh_lighting_server_cb_evt_to_btc(
  1673. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  1674. return;
  1675. }
  1676. switch (ctx->recv_op) {
  1677. case BLE_MESH_MODEL_OP_LIGHT_XYL_GET:
  1678. opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS;
  1679. break;
  1680. case BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET:
  1681. opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS;
  1682. break;
  1683. case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET:
  1684. opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS;
  1685. break;
  1686. case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET:
  1687. opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS;
  1688. break;
  1689. default:
  1690. BT_WARN("Unknown Light xyL Get opcode 0x%04x", ctx->recv_op);
  1691. return;
  1692. }
  1693. send_light_xyl_status(model, ctx, false, opcode);
  1694. return;
  1695. }
  1696. void light_xyl_publish(struct bt_mesh_model *model, uint16_t opcode)
  1697. {
  1698. if (model->user_data == NULL) {
  1699. BT_ERR("%s, Invalid model user data", __func__);
  1700. return;
  1701. }
  1702. switch (model->id) {
  1703. case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: {
  1704. struct bt_mesh_light_xyl_srv *srv = model->user_data;
  1705. if (srv->state == NULL) {
  1706. BT_ERR("Invalid Light xyL Server state");
  1707. return;
  1708. }
  1709. break;
  1710. }
  1711. case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV: {
  1712. struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
  1713. if (srv->state == NULL) {
  1714. BT_ERR("Invalid Light xyL Setup Server state");
  1715. return;
  1716. }
  1717. break;
  1718. }
  1719. default:
  1720. BT_ERR("Invalid Light xyL Server model 0x%04x", model->id);
  1721. return;
  1722. }
  1723. send_light_xyl_status(model, NULL, true, opcode);
  1724. return;
  1725. }
  1726. static void light_xyl_set(struct bt_mesh_model *model,
  1727. struct bt_mesh_msg_ctx *ctx,
  1728. struct net_buf_simple *buf)
  1729. {
  1730. struct bt_mesh_light_xyl_srv *srv = model->user_data;
  1731. uint8_t tid = 0U, trans_time = 0U, delay = 0U;
  1732. uint16_t lightness = 0U, x = 0U, y = 0U;
  1733. bool optional = false;
  1734. int64_t now = 0;
  1735. if (srv == NULL || srv->state == NULL) {
  1736. BT_ERR("%s, Invalid model user data", __func__);
  1737. return;
  1738. }
  1739. lightness = net_buf_simple_pull_le16(buf);
  1740. x = net_buf_simple_pull_le16(buf);
  1741. y = net_buf_simple_pull_le16(buf);
  1742. tid = net_buf_simple_pull_u8(buf);
  1743. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  1744. return;
  1745. }
  1746. /* Callback the received message to the application layer */
  1747. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1748. bt_mesh_light_server_recv_set_msg_t set = {
  1749. .xyl_set.op_en = optional,
  1750. .xyl_set.lightness = lightness,
  1751. .xyl_set.x = x,
  1752. .xyl_set.y = y,
  1753. .xyl_set.tid = tid,
  1754. .xyl_set.trans_time = trans_time,
  1755. .xyl_set.delay = delay,
  1756. };
  1757. bt_mesh_lighting_server_cb_evt_to_btc(
  1758. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  1759. return;
  1760. }
  1761. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  1762. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) {
  1763. send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
  1764. }
  1765. send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
  1766. /* In this condition, no event will be callback to application layer */
  1767. return;
  1768. }
  1769. bt_mesh_light_server_lock();
  1770. bt_mesh_server_stop_transition(&srv->transition);
  1771. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  1772. srv->state->target_lightness = lightness;
  1773. if (srv->state->x_range_min && x < srv->state->x_range_min) {
  1774. x = srv->state->x_range_min;
  1775. } else if (srv->state->x_range_max && x > srv->state->x_range_max) {
  1776. x = srv->state->x_range_max;
  1777. }
  1778. srv->state->target_x = x;
  1779. if (srv->state->y_range_min && y < srv->state->y_range_min) {
  1780. y = srv->state->y_range_min;
  1781. } else if (srv->state->y_range_max && y > srv->state->y_range_max) {
  1782. y = srv->state->y_range_max;
  1783. }
  1784. srv->state->target_y = y;
  1785. /**
  1786. * If the target state is equal to the current state, the transition shall not
  1787. * be started and is considered complete.
  1788. */
  1789. if (srv->state->target_lightness != srv->state->lightness ||
  1790. srv->state->target_x != srv->state->x ||
  1791. srv->state->target_y != srv->state->y) {
  1792. light_xyl_tt_values(srv, trans_time, delay);
  1793. } else {
  1794. bt_mesh_light_server_state_change_t change = {
  1795. .xyl_set.lightness = srv->state->lightness,
  1796. .xyl_set.x = srv->state->x,
  1797. .xyl_set.y = srv->state->y,
  1798. };
  1799. bt_mesh_lighting_server_cb_evt_to_btc(
  1800. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  1801. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) {
  1802. send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
  1803. }
  1804. send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
  1805. bt_mesh_light_server_unlock();
  1806. return;
  1807. }
  1808. /* Copy the ctx of the received message */
  1809. if (srv->transition.timer.work._reserved) {
  1810. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  1811. }
  1812. /* For Instantaneous Transition */
  1813. if (srv->transition.counter == 0U) {
  1814. srv->state->lightness = srv->state->target_lightness;
  1815. srv->state->x = srv->state->target_x;
  1816. srv->state->y = srv->state->target_y;
  1817. }
  1818. srv->transition.just_started = true;
  1819. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) {
  1820. send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
  1821. }
  1822. send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
  1823. bt_mesh_light_server_unlock();
  1824. bt_mesh_server_start_transition(&srv->transition);
  1825. return;
  1826. }
  1827. static void light_xyl_default_set(struct bt_mesh_model *model,
  1828. struct bt_mesh_msg_ctx *ctx,
  1829. struct net_buf_simple *buf)
  1830. {
  1831. struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
  1832. uint16_t lightness = 0U, x = 0U, y = 0U;
  1833. if (srv == NULL || srv->state == NULL) {
  1834. BT_ERR("%s, Invalid model user data", __func__);
  1835. return;
  1836. }
  1837. lightness = net_buf_simple_pull_le16(buf);
  1838. x = net_buf_simple_pull_le16(buf);
  1839. y = net_buf_simple_pull_le16(buf);
  1840. /* Callback the received message to the application layer */
  1841. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1842. bt_mesh_light_server_recv_set_msg_t set = {
  1843. .xyl_default_set.lightness = lightness,
  1844. .xyl_default_set.x = x,
  1845. .xyl_default_set.y = y,
  1846. };
  1847. bt_mesh_lighting_server_cb_evt_to_btc(
  1848. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  1849. return;
  1850. }
  1851. if (srv->state->x_range_min && x < srv->state->x_range_min) {
  1852. x = srv->state->x_range_min;
  1853. } else if (srv->state->x_range_max && x > srv->state->x_range_max) {
  1854. x = srv->state->x_range_max;
  1855. }
  1856. if (srv->state->y_range_min && y < srv->state->y_range_min) {
  1857. y = srv->state->y_range_min;
  1858. } else if (srv->state->y_range_max && y > srv->state->y_range_max) {
  1859. y = srv->state->y_range_max;
  1860. }
  1861. srv->state->lightness_default = lightness;
  1862. srv->state->x_default = x;
  1863. srv->state->y_default = y;
  1864. bt_mesh_light_server_state_change_t change = {
  1865. .xyl_default_set.lightness = srv->state->lightness_default,
  1866. .xyl_default_set.x = srv->state->x_default,
  1867. .xyl_default_set.y = srv->state->y_default,
  1868. };
  1869. bt_mesh_lighting_server_cb_evt_to_btc(
  1870. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  1871. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET) {
  1872. send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS);
  1873. }
  1874. send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS);
  1875. return;
  1876. }
  1877. static void light_xyl_range_set(struct bt_mesh_model *model,
  1878. struct bt_mesh_msg_ctx *ctx,
  1879. struct net_buf_simple *buf)
  1880. {
  1881. struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
  1882. uint16_t x_min = 0U, x_max = 0U, y_min = 0U, y_max = 0U;
  1883. if (srv == NULL || srv->state == NULL) {
  1884. BT_ERR("%s, Invalid model user data", __func__);
  1885. return;
  1886. }
  1887. x_min = net_buf_simple_pull_le16(buf);
  1888. x_max = net_buf_simple_pull_le16(buf);
  1889. y_min = net_buf_simple_pull_le16(buf);
  1890. y_max = net_buf_simple_pull_le16(buf);
  1891. if (x_min > x_max) {
  1892. BT_ERR("Invalid parameter, x min 0x%04x, x max 0x%04x",
  1893. x_min, x_max);
  1894. return;
  1895. }
  1896. if (y_min > y_max) {
  1897. BT_ERR("Invalid parameter, y min 0x%04x, y max 0x%04x",
  1898. y_min, y_max);
  1899. return;
  1900. }
  1901. /* Callback the received message to the application layer */
  1902. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1903. bt_mesh_light_server_recv_set_msg_t set = {
  1904. .xyl_range_set.x_range_min = x_min,
  1905. .xyl_range_set.x_range_max = x_max,
  1906. .xyl_range_set.y_range_min = y_min,
  1907. .xyl_range_set.y_range_max = y_max,
  1908. };
  1909. bt_mesh_lighting_server_cb_evt_to_btc(
  1910. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  1911. return;
  1912. }
  1913. srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
  1914. srv->state->x_range_min = x_min;
  1915. srv->state->x_range_max = x_max;
  1916. srv->state->y_range_min = y_min;
  1917. srv->state->y_range_max = y_max;
  1918. bt_mesh_light_server_state_change_t change = {
  1919. .xyl_range_set.x_range_min = srv->state->x_range_min,
  1920. .xyl_range_set.x_range_max = srv->state->x_range_max,
  1921. .xyl_range_set.y_range_min = srv->state->y_range_min,
  1922. .xyl_range_set.y_range_max = srv->state->y_range_max,
  1923. };
  1924. bt_mesh_lighting_server_cb_evt_to_btc(
  1925. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  1926. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET) {
  1927. send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS);
  1928. }
  1929. send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS);
  1930. return;
  1931. }
  1932. /* Light LC Server/Setup Server message handlers */
  1933. static void send_light_lc_status(struct bt_mesh_model *model,
  1934. struct bt_mesh_msg_ctx *ctx,
  1935. bool publish, uint16_t opcode)
  1936. {
  1937. struct bt_mesh_light_lc_srv *srv = model->user_data;
  1938. struct net_buf_simple *msg = NULL;
  1939. uint8_t length = 2 + 3;
  1940. if (ctx == NULL && publish == false) {
  1941. BT_ERR("%s, Invalid parameter", __func__);
  1942. return;
  1943. }
  1944. if (publish == false) {
  1945. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  1946. if (msg == NULL) {
  1947. BT_ERR("%s, Out of memory", __func__);
  1948. return;
  1949. }
  1950. } else {
  1951. msg = bt_mesh_server_get_pub_msg(model, length);
  1952. if (msg == NULL) {
  1953. return;
  1954. }
  1955. }
  1956. bt_mesh_model_msg_init(msg, opcode);
  1957. switch (opcode) {
  1958. case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS:
  1959. net_buf_simple_add_u8(msg, srv->lc->state.mode);
  1960. break;
  1961. case BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS:
  1962. net_buf_simple_add_u8(msg, srv->lc->state.occupancy_mode);
  1963. break;
  1964. case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS:
  1965. net_buf_simple_add_u8(msg, srv->lc->state.light_onoff);
  1966. if (srv->transition.counter) {
  1967. bt_mesh_server_calc_remain_time(&srv->transition);
  1968. net_buf_simple_add_u8(msg, srv->lc->state.target_light_onoff);
  1969. net_buf_simple_add_u8(msg, srv->transition.remain_time);
  1970. }
  1971. break;
  1972. default:
  1973. BT_WARN("Unknown Light LC status opcode 0x%04x", opcode);
  1974. if (publish == false) {
  1975. bt_mesh_free_buf(msg);
  1976. }
  1977. return;
  1978. }
  1979. if (publish == false) {
  1980. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  1981. bt_mesh_free_buf(msg);
  1982. } else {
  1983. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  1984. }
  1985. return;
  1986. }
  1987. static void light_lc_get(struct bt_mesh_model *model,
  1988. struct bt_mesh_msg_ctx *ctx,
  1989. struct net_buf_simple *buf)
  1990. {
  1991. struct bt_mesh_light_lc_srv *srv = model->user_data;
  1992. uint16_t opcode = 0U;
  1993. if (srv == NULL || srv->lc == NULL) {
  1994. BT_ERR("%s, Invalid model user data", __func__);
  1995. return;
  1996. }
  1997. /* Callback the received message to the application layer */
  1998. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  1999. bt_mesh_lighting_server_cb_evt_to_btc(
  2000. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
  2001. return;
  2002. }
  2003. switch (ctx->recv_op) {
  2004. case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET:
  2005. opcode = BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS;
  2006. break;
  2007. case BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET:
  2008. opcode = BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS;
  2009. break;
  2010. case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET:
  2011. opcode = BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS;
  2012. break;
  2013. default:
  2014. BT_WARN("Unknown Light LC Get opcode 0x%04x", ctx->recv_op);
  2015. return;
  2016. }
  2017. send_light_lc_status(model, ctx, false, opcode);
  2018. return;
  2019. }
  2020. void light_lc_publish(struct bt_mesh_model *model, uint16_t opcode)
  2021. {
  2022. struct bt_mesh_light_lc_srv *srv = model->user_data;
  2023. if (srv == NULL || srv->lc == NULL) {
  2024. BT_ERR("%s, Invalid model user data", __func__);
  2025. return;
  2026. }
  2027. send_light_lc_status(model, NULL, true, opcode);
  2028. return;
  2029. }
  2030. static void light_lc_mode_set(struct bt_mesh_model *model,
  2031. struct bt_mesh_msg_ctx *ctx,
  2032. struct net_buf_simple *buf)
  2033. {
  2034. struct bt_mesh_light_lc_srv *srv = model->user_data;
  2035. uint8_t mode = 0U;
  2036. if (srv == NULL || srv->lc == NULL) {
  2037. BT_ERR("%s, Invalid model user data", __func__);
  2038. return;
  2039. }
  2040. mode = net_buf_simple_pull_u8(buf);
  2041. if (mode > BLE_MESH_STATE_ON) {
  2042. BT_ERR("Invalid LC Mode 0x%02x", mode);
  2043. return;
  2044. }
  2045. /* Callback the received message to the application layer */
  2046. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  2047. bt_mesh_light_server_recv_set_msg_t set = {
  2048. .lc_mode_set.mode = mode,
  2049. };
  2050. bt_mesh_lighting_server_cb_evt_to_btc(
  2051. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  2052. return;
  2053. }
  2054. srv->lc->state.mode = mode;
  2055. bt_mesh_light_server_state_change_t change = {
  2056. .lc_mode_set.mode = srv->lc->state.mode,
  2057. };
  2058. bt_mesh_lighting_server_cb_evt_to_btc(
  2059. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  2060. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET) {
  2061. send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS);
  2062. }
  2063. send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS);
  2064. return;
  2065. }
  2066. static void light_lc_om_set(struct bt_mesh_model *model,
  2067. struct bt_mesh_msg_ctx *ctx,
  2068. struct net_buf_simple *buf)
  2069. {
  2070. struct bt_mesh_light_lc_srv *srv = model->user_data;
  2071. uint8_t om = 0U;
  2072. if (srv == NULL || srv->lc == NULL) {
  2073. BT_ERR("%s, Invalid model user data", __func__);
  2074. return;
  2075. }
  2076. om = net_buf_simple_pull_u8(buf);
  2077. if (om > BLE_MESH_STATE_ON) {
  2078. BT_ERR("Invalid LC Occupancy Mode 0x%02x", om);
  2079. return;
  2080. }
  2081. /* Callback the received message to the application layer */
  2082. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  2083. bt_mesh_light_server_recv_set_msg_t set = {
  2084. .lc_om_set.mode = om,
  2085. };
  2086. bt_mesh_lighting_server_cb_evt_to_btc(
  2087. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  2088. return;
  2089. }
  2090. srv->lc->state.occupancy_mode = om;
  2091. bt_mesh_light_server_state_change_t change = {
  2092. .lc_om_set.mode = srv->lc->state.occupancy_mode,
  2093. };
  2094. bt_mesh_lighting_server_cb_evt_to_btc(
  2095. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  2096. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET) {
  2097. send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS);
  2098. }
  2099. send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS);
  2100. return;
  2101. }
  2102. static void light_lc_light_onoff_set(struct bt_mesh_model *model,
  2103. struct bt_mesh_msg_ctx *ctx,
  2104. struct net_buf_simple *buf)
  2105. {
  2106. struct bt_mesh_light_lc_srv *srv = model->user_data;
  2107. uint8_t tid = 0U, trans_time = 0U, delay = 0U;
  2108. bool optional = false;
  2109. uint8_t onoff = 0U;
  2110. int64_t now = 0;
  2111. if (srv == NULL || srv->lc == NULL) {
  2112. BT_ERR("%s, Invalid model user data", __func__);
  2113. return;
  2114. }
  2115. onoff = net_buf_simple_pull_u8(buf);
  2116. tid = net_buf_simple_pull_u8(buf);
  2117. if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
  2118. return;
  2119. }
  2120. /* Callback the received message to the application layer */
  2121. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  2122. bt_mesh_light_server_recv_set_msg_t set = {
  2123. .lc_light_onoff_set.op_en = optional,
  2124. .lc_light_onoff_set.light_onoff = onoff,
  2125. .lc_light_onoff_set.tid = tid,
  2126. .lc_light_onoff_set.trans_time = trans_time,
  2127. .lc_light_onoff_set.delay = delay,
  2128. };
  2129. bt_mesh_lighting_server_cb_evt_to_btc(
  2130. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  2131. return;
  2132. }
  2133. if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
  2134. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) {
  2135. send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
  2136. }
  2137. send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
  2138. /* In this condition, no event will be callback to application layer */
  2139. return;
  2140. }
  2141. bt_mesh_light_server_lock();
  2142. bt_mesh_server_stop_transition(&srv->transition);
  2143. bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
  2144. srv->lc->state.target_light_onoff = onoff;
  2145. if (srv->lc->state.target_light_onoff != srv->lc->state.light_onoff) {
  2146. light_lc_tt_values(srv, trans_time, delay);
  2147. } else {
  2148. bt_mesh_light_server_state_change_t change = {
  2149. .lc_light_onoff_set.onoff = srv->lc->state.light_onoff,
  2150. };
  2151. bt_mesh_lighting_server_cb_evt_to_btc(
  2152. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  2153. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) {
  2154. send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
  2155. }
  2156. send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
  2157. bt_mesh_light_server_unlock();
  2158. return;
  2159. }
  2160. /* Copy the ctx of the received message */
  2161. if (srv->transition.timer.work._reserved) {
  2162. memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
  2163. }
  2164. /* For Instantaneous Transition */
  2165. if (srv->transition.counter == 0U) {
  2166. srv->lc->state.light_onoff = srv->lc->state.target_light_onoff;
  2167. }
  2168. srv->transition.just_started = true;
  2169. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) {
  2170. send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
  2171. }
  2172. send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
  2173. bt_mesh_light_server_unlock();
  2174. bt_mesh_server_start_transition(&srv->transition);
  2175. return;
  2176. }
  2177. static void light_lc_sensor_status(struct bt_mesh_model *model,
  2178. struct bt_mesh_msg_ctx *ctx,
  2179. struct net_buf_simple *buf)
  2180. {
  2181. /**
  2182. * When a Light LC Server receives a Sensor Status message, and if the message
  2183. * Raw field contains a Raw Value for the Motion Sensed Property, and the value
  2184. * is greater than 0, or a Raw Value for the People Count Property, and the
  2185. * value is greater than 0, or a Raw Value for the Presence Detected Property,
  2186. * and the value is greater than 0, then it shall set the Light LC Occupancy
  2187. * state to 0b1.
  2188. * If the message Raw field contains a Raw Value for the Time Since Motion Sensed
  2189. * device property, which represents a value less than or equal to the value of
  2190. * the Light LC Occupancy Delay state, it shall delay setting the Light LC Occupancy
  2191. * state to 0b1 by the difference between the value of the Light LC Occupancy Delay
  2192. * state and the received Time Since Motion value.
  2193. * When a Light LC Server receives a Sensor Status message, and if the message Raw
  2194. * field contains a Raw Value for the Present Ambient Light Level device property,
  2195. * it shall set the Light LC Ambient LuxLevel state to the Represented Value of the
  2196. * received Present Ambient Light Level.
  2197. *
  2198. * Motion Sensed: 1 octet, 0x0042
  2199. * People Count: 2 octets, 0x004C
  2200. * Presence Detected: 1 octet, 0x004D
  2201. *
  2202. * Time Since Motion Sensed: 2 octets, 0x0068
  2203. *
  2204. * Present Ambient Light Level: 4 octets, 0x004E
  2205. */
  2206. struct bt_mesh_light_lc_srv *srv = model->user_data;
  2207. bt_mesh_light_server_state_change_t change = {0};
  2208. uint16_t mpid = 0U, prop_id = 0U;
  2209. uint8_t length = 0U;
  2210. if (srv == NULL || srv->lc == NULL) {
  2211. BT_ERR("%s, Invalid model user data", __func__);
  2212. return;
  2213. }
  2214. if (srv->rsp_ctrl.status_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  2215. bt_mesh_light_server_recv_status_msg_t status = {
  2216. .sensor_status.data = buf,
  2217. };
  2218. bt_mesh_lighting_server_cb_evt_to_btc(
  2219. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_STATUS_MSG, model, ctx, (const uint8_t *)&status, sizeof(status));
  2220. return;
  2221. }
  2222. mpid = net_buf_simple_pull_le16(buf);
  2223. if (mpid & BIT(0)) {
  2224. length = (uint8_t)((mpid & 0xff) >> 1);
  2225. uint8_t msb = net_buf_simple_pull_u8(buf);
  2226. prop_id = (uint16_t)(msb << 8) | (uint16_t)(mpid >> 8);
  2227. } else {
  2228. length = (uint8_t)((mpid & 0x1f) >> 1);
  2229. prop_id = (uint16_t)(mpid >> 5);
  2230. }
  2231. change.sensor_status.property_id = prop_id;
  2232. switch (prop_id) {
  2233. case BLE_MESH_MOTION_SENSED: {
  2234. if (length != BLE_MESH_MOTION_SENSED_LEN || length != buf->len) {
  2235. BT_WARN("Invalid Motion Sensed Property length %d", length);
  2236. return;
  2237. }
  2238. uint8_t val = net_buf_simple_pull_u8(buf);
  2239. if (val > 0) {
  2240. srv->lc->state.occupancy = BLE_MESH_STATE_ON;
  2241. change.sensor_status.state.occupancy = srv->lc->state.occupancy;
  2242. bt_mesh_lighting_server_cb_evt_to_btc(
  2243. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  2244. }
  2245. break;
  2246. }
  2247. case BLE_MESH_PEOPLE_COUNT: {
  2248. if (length != BLE_MESH_PEOPLE_COUNT_LEN || length != buf->len) {
  2249. BT_WARN("Invalid Motion Sensed Property length %d", length);
  2250. return;
  2251. }
  2252. uint16_t val = net_buf_simple_pull_le16(buf);
  2253. if (val > 0) {
  2254. srv->lc->state.occupancy = BLE_MESH_STATE_ON;
  2255. change.sensor_status.state.occupancy = srv->lc->state.occupancy;
  2256. bt_mesh_lighting_server_cb_evt_to_btc(
  2257. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  2258. }
  2259. break;
  2260. }
  2261. case BLE_MESH_PRESENCE_DETECTED: {
  2262. if (length != BLE_MESH_PRESENCE_DETECTED_LEN || length != buf->len) {
  2263. BT_WARN("Invalid Motion Sensed Property length %d", length);
  2264. return;
  2265. }
  2266. uint8_t val = net_buf_simple_pull_u8(buf);
  2267. if (val > 0) {
  2268. srv->lc->state.occupancy = BLE_MESH_STATE_ON;
  2269. change.sensor_status.state.occupancy = srv->lc->state.occupancy;
  2270. bt_mesh_lighting_server_cb_evt_to_btc(
  2271. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  2272. }
  2273. break;
  2274. }
  2275. case BLE_MESH_TIME_SINCE_MOTION_SENSED: {
  2276. if (length != BLE_MESH_TIME_SINCE_MOTION_SENSED_LEN || length != buf->len) {
  2277. BT_WARN("Invalid Motion Sensed Property length %d", length);
  2278. return;
  2279. }
  2280. uint16_t val = net_buf_simple_pull_le16(buf);
  2281. if (val <= srv->lc->prop_state.time_occupancy_delay) {
  2282. srv->lc->prop_state.set_occupancy_to_1_delay =
  2283. srv->lc->prop_state.time_occupancy_delay - val;
  2284. change.sensor_status.state.set_occupancy_to_1_delay = srv->lc->prop_state.set_occupancy_to_1_delay;
  2285. bt_mesh_lighting_server_cb_evt_to_btc(
  2286. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  2287. }
  2288. break;
  2289. }
  2290. case BLE_MESH_PRESENT_AMBIENT_LIGHT_LEVEL: {
  2291. /**
  2292. * Present Ambient Light Level device property is 4 octets, but ambient
  2293. * luxlevel length is 3 octets, and other devices may send Sensor Status
  2294. * which only contains 3 octets just for Light LC Server.
  2295. * Here we just check if the length is larger than 3.
  2296. */
  2297. if (buf->len < 3) {
  2298. BT_WARN("Invalid Motion Sensed Property length %d", buf->len);
  2299. return;
  2300. }
  2301. uint16_t lsb = net_buf_simple_pull_le16(buf);
  2302. uint8_t msb = net_buf_simple_pull_u8(buf);
  2303. srv->lc->state.ambient_luxlevel = (msb << 16) | lsb;
  2304. change.sensor_status.state.ambient_luxlevel = srv->lc->state.ambient_luxlevel;
  2305. bt_mesh_lighting_server_cb_evt_to_btc(
  2306. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  2307. break;
  2308. }
  2309. default:
  2310. break;
  2311. }
  2312. }
  2313. static uint8_t *get_light_lc_prop_val(struct bt_mesh_model *model, uint16_t prop_id)
  2314. {
  2315. struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
  2316. uint8_t *val = NULL;
  2317. switch (prop_id) {
  2318. case BLE_MESH_LIGHT_CONTROL_TIME_OCCUPANCY_DELAY:
  2319. val = (uint8_t *)&srv->lc->prop_state.time_occupancy_delay;
  2320. break;
  2321. case BLE_MESH_LIGHT_CONTROL_TIME_FADE_ON:
  2322. val = (uint8_t *)&srv->lc->prop_state.time_fade_on;
  2323. break;
  2324. case BLE_MESH_LIGHT_CONTROL_TIME_RUN_ON:
  2325. val = (uint8_t *)&srv->lc->prop_state.time_run_on;
  2326. break;
  2327. case BLE_MESH_LIGHT_CONTROL_TIME_FADE:
  2328. val = (uint8_t *)&srv->lc->prop_state.time_fade;
  2329. break;
  2330. case BLE_MESH_LIGHT_CONTROL_TIME_PROLONG:
  2331. val = (uint8_t *)&srv->lc->prop_state.time_prolong;
  2332. break;
  2333. case BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_AUTO:
  2334. val = (uint8_t *)&srv->lc->prop_state.time_fade_standby_auto;
  2335. break;
  2336. case BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_MANUAL:
  2337. val = (uint8_t *)&srv->lc->prop_state.time_fade_standby_manual;
  2338. break;
  2339. case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_ON:
  2340. val = (uint8_t *)&srv->lc->prop_state.lightness_on;
  2341. break;
  2342. case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_PROLONG:
  2343. val = (uint8_t *)&srv->lc->prop_state.lightness_prolong;
  2344. break;
  2345. case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_STANDBY:
  2346. val = (uint8_t *)&srv->lc->prop_state.lightness_standby;
  2347. break;
  2348. case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_ON:
  2349. val = (uint8_t *)&srv->lc->prop_state.ambient_luxlevel_on;
  2350. break;
  2351. case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_PROLONG:
  2352. val = (uint8_t *)&srv->lc->prop_state.ambient_luxlevel_prolong;
  2353. break;
  2354. case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_STANDBY:
  2355. val = (uint8_t *)&srv->lc->prop_state.ambient_luxlevel_standby;
  2356. break;
  2357. case BLE_MESH_LIGHT_CONTROL_REGULATOR_KIU:
  2358. val = (uint8_t *)&srv->lc->prop_state.regulator_kiu;
  2359. break;
  2360. case BLE_MESH_LIGHT_CONTROL_REGULATOR_KID:
  2361. val = (uint8_t *)&srv->lc->prop_state.regulator_kid;
  2362. break;
  2363. case BLE_MESH_LIGHT_CONTROL_REGULATOR_KPU:
  2364. val = (uint8_t *)&srv->lc->prop_state.regulator_kpu;
  2365. break;
  2366. case BLE_MESH_LIGHT_CONTROL_REGULATOR_KPD:
  2367. val = (uint8_t *)&srv->lc->prop_state.regulator_kpd;
  2368. break;
  2369. case BLE_MESH_LIGHT_CONTROL_REGULATOR_ACCURACY:
  2370. val = (uint8_t *)&srv->lc->prop_state.regulator_accuracy;
  2371. break;
  2372. }
  2373. return val;
  2374. }
  2375. uint8_t *bt_mesh_get_lc_prop_value(struct bt_mesh_model *model, uint16_t prop_id)
  2376. {
  2377. if (model == NULL) {
  2378. BT_ERR("%s, Invalid parameter", __func__);
  2379. return NULL;
  2380. }
  2381. return get_light_lc_prop_val(model, prop_id);
  2382. }
  2383. static void send_light_lc_prop_status(struct bt_mesh_model *model,
  2384. struct bt_mesh_msg_ctx *ctx,
  2385. uint16_t prop_id, bool publish)
  2386. {
  2387. struct net_buf_simple *msg = NULL;
  2388. uint8_t length = 1 + 2 + 4;
  2389. uint8_t *prop_val = NULL;
  2390. prop_val = get_light_lc_prop_val(model, prop_id);
  2391. if (prop_val == NULL) {
  2392. BT_ERR("Failed to get Light LC Property value");
  2393. return;
  2394. }
  2395. if (publish == false) {
  2396. msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  2397. if (msg == NULL) {
  2398. BT_ERR("%s, Out of memory", __func__);
  2399. return;
  2400. }
  2401. } else {
  2402. msg = bt_mesh_server_get_pub_msg(model, length);
  2403. if (msg == NULL) {
  2404. return;
  2405. }
  2406. }
  2407. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS);
  2408. net_buf_simple_add_le16(msg, prop_id);
  2409. net_buf_simple_add_mem(msg, prop_val, bt_mesh_get_dev_prop_len(prop_id));
  2410. if (publish == false) {
  2411. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  2412. bt_mesh_free_buf(msg);
  2413. } else {
  2414. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  2415. }
  2416. return;
  2417. }
  2418. static void light_lc_prop_get(struct bt_mesh_model *model,
  2419. struct bt_mesh_msg_ctx *ctx,
  2420. struct net_buf_simple *buf)
  2421. {
  2422. struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
  2423. uint16_t prop_id = 0U;
  2424. if (srv == NULL || srv->lc == NULL) {
  2425. BT_ERR("%s, Invalid model user data", __func__);
  2426. return;
  2427. }
  2428. prop_id = net_buf_simple_pull_le16(buf);
  2429. if (prop_id < 0x002B || prop_id > 0x003C) {
  2430. BT_ERR("Invalid Light LC Property ID 0x%04x", prop_id);
  2431. return;
  2432. }
  2433. /* Callback the received message to the application layer */
  2434. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  2435. bt_mesh_light_server_recv_get_msg_t get = {
  2436. .lc_property_get.id = net_buf_simple_pull_le16(buf),
  2437. };
  2438. bt_mesh_lighting_server_cb_evt_to_btc(
  2439. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get));
  2440. return;
  2441. }
  2442. send_light_lc_prop_status(model, ctx, prop_id, false);
  2443. return;
  2444. }
  2445. static void light_lc_prop_set(struct bt_mesh_model *model,
  2446. struct bt_mesh_msg_ctx *ctx,
  2447. struct net_buf_simple *buf)
  2448. {
  2449. struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
  2450. uint8_t *prop_val = NULL, expect_len = 0U;
  2451. uint16_t prop_id = 0U;
  2452. if (srv == NULL || srv->lc == NULL) {
  2453. BT_ERR("%s, Invalid model user data", __func__);
  2454. return;
  2455. }
  2456. prop_id = net_buf_simple_pull_le16(buf);
  2457. if (prop_id < 0x002B || prop_id > 0x003C) {
  2458. BT_ERR("Invalid Light LC Property ID 0x%04x", prop_id);
  2459. return;
  2460. }
  2461. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  2462. bt_mesh_light_server_recv_set_msg_t set = {
  2463. .lc_property_set.id = net_buf_simple_pull_le16(buf),
  2464. .lc_property_set.value = buf,
  2465. };
  2466. bt_mesh_lighting_server_cb_evt_to_btc(
  2467. BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
  2468. return;
  2469. }
  2470. expect_len = bt_mesh_get_dev_prop_len(prop_id);
  2471. if (buf->len != expect_len) {
  2472. BT_ERR("Invalid Light LC Property 0x%04x length, expect %d, actual %d",
  2473. prop_id, expect_len, buf->len);
  2474. return;
  2475. }
  2476. prop_val = get_light_lc_prop_val(model, prop_id);
  2477. if (prop_val == NULL) {
  2478. BT_ERR("Failed to get Light LC Property value");
  2479. return;
  2480. }
  2481. memcpy(prop_val, buf->data, buf->len);
  2482. bt_mesh_light_server_state_change_t change = {
  2483. .lc_property_set.id = prop_id,
  2484. .lc_property_set.value = buf,
  2485. };
  2486. bt_mesh_lighting_server_cb_evt_to_btc(
  2487. BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
  2488. if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET) {
  2489. send_light_lc_prop_status(model, ctx, prop_id, false);
  2490. }
  2491. send_light_lc_prop_status(model, ctx, prop_id, true);
  2492. return;
  2493. }
  2494. /* message handlers (End) */
  2495. /* Mapping of message handlers for Light Lightness Server (0x1300) */
  2496. const struct bt_mesh_model_op bt_mesh_light_lightness_srv_op[] = {
  2497. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET, 0, light_lightness_get },
  2498. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET, 3, light_lightness_set },
  2499. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK, 3, light_lightness_set },
  2500. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET, 0, light_lightness_get },
  2501. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET, 3, light_lightness_linear_set },
  2502. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK, 3, light_lightness_linear_set },
  2503. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET, 0, light_lightness_get },
  2504. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET, 0, light_lightness_get },
  2505. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET, 0, light_lightness_get },
  2506. BLE_MESH_MODEL_OP_END,
  2507. };
  2508. /* Mapping of message handlers for Light Lightness Setup Server (0x1301) */
  2509. const struct bt_mesh_model_op bt_mesh_light_lightness_setup_srv_op[] = {
  2510. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET, 2, light_lightness_default_set },
  2511. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK, 2, light_lightness_default_set },
  2512. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET, 4, light_lightness_range_set },
  2513. { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK, 4, light_lightness_range_set },
  2514. BLE_MESH_MODEL_OP_END,
  2515. };
  2516. /* Mapping of message handlers for Light CTL Server (0x1303) */
  2517. const struct bt_mesh_model_op bt_mesh_light_ctl_srv_op[] = {
  2518. { BLE_MESH_MODEL_OP_LIGHT_CTL_GET, 0, light_ctl_get },
  2519. { BLE_MESH_MODEL_OP_LIGHT_CTL_SET, 7, light_ctl_set },
  2520. { BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK, 7, light_ctl_set },
  2521. { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET, 0, light_ctl_get },
  2522. { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET, 0, light_ctl_get },
  2523. BLE_MESH_MODEL_OP_END,
  2524. };
  2525. /* Mapping of message handlers for Light CTL Setup Server (0x1304) */
  2526. const struct bt_mesh_model_op bt_mesh_light_ctl_setup_srv_op[] = {
  2527. { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET, 6, light_ctl_default_set },
  2528. { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK, 6, light_ctl_default_set },
  2529. { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET, 4, light_ctl_temp_range_set },
  2530. { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK, 4, light_ctl_temp_range_set },
  2531. BLE_MESH_MODEL_OP_END,
  2532. };
  2533. /* Mapping of message handlers for Light CTL Temperature Server (0x1306) */
  2534. const struct bt_mesh_model_op bt_mesh_light_ctl_temp_srv_op[] = {
  2535. { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET, 0, light_ctl_get },
  2536. { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET, 5, light_ctl_temp_set },
  2537. { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK, 5, light_ctl_temp_set },
  2538. BLE_MESH_MODEL_OP_END,
  2539. };
  2540. /* Mapping of message handlers for Light HSL Server (0x1307) */
  2541. const struct bt_mesh_model_op bt_mesh_light_hsl_srv_op[] = {
  2542. { BLE_MESH_MODEL_OP_LIGHT_HSL_GET, 0, light_hsl_get },
  2543. { BLE_MESH_MODEL_OP_LIGHT_HSL_SET, 7, light_hsl_set },
  2544. { BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK, 7, light_hsl_set },
  2545. { BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET, 0, light_hsl_get },
  2546. { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET, 0, light_hsl_get },
  2547. { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET, 0, light_hsl_get },
  2548. BLE_MESH_MODEL_OP_END,
  2549. };
  2550. /* Mapping of message handlers for Light HSL Setup Server (0x1308) */
  2551. const struct bt_mesh_model_op bt_mesh_light_hsl_setup_srv_op[] = {
  2552. { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET, 6, light_hsl_default_set },
  2553. { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK, 6, light_hsl_default_set },
  2554. { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET, 8, light_hsl_range_set },
  2555. { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK, 8, light_hsl_range_set },
  2556. BLE_MESH_MODEL_OP_END,
  2557. };
  2558. /* Mapping of message handlers for Light HSL Hue Server (0x130A) */
  2559. const struct bt_mesh_model_op bt_mesh_light_hsl_hue_srv_op[] = {
  2560. { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET, 0, light_hsl_get },
  2561. { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET, 3, light_hsl_hue_set },
  2562. { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK, 3, light_hsl_hue_set },
  2563. BLE_MESH_MODEL_OP_END,
  2564. };
  2565. /* Mapping of message handlers for Light HSL Saturation Server (0x130B) */
  2566. const struct bt_mesh_model_op bt_mesh_light_hsl_sat_srv_op[] = {
  2567. { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET, 0, light_hsl_get },
  2568. { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET, 3, light_hsl_sat_set },
  2569. { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK, 3, light_hsl_sat_set },
  2570. BLE_MESH_MODEL_OP_END,
  2571. };
  2572. /* Mapping of message handlers for Light xyL Server (0x130C) */
  2573. const struct bt_mesh_model_op bt_mesh_light_xyl_srv_op[] = {
  2574. { BLE_MESH_MODEL_OP_LIGHT_XYL_GET, 0, light_xyl_get },
  2575. { BLE_MESH_MODEL_OP_LIGHT_XYL_SET, 7, light_xyl_set },
  2576. { BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK, 7, light_xyl_set },
  2577. { BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET, 0, light_xyl_get },
  2578. { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET, 0, light_xyl_get },
  2579. { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET, 0, light_xyl_get },
  2580. BLE_MESH_MODEL_OP_END,
  2581. };
  2582. /* Mapping of message handlers for Light xyL Setup Server (0x130D) */
  2583. const struct bt_mesh_model_op bt_mesh_light_xyl_setup_srv_op[] = {
  2584. { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET, 6, light_xyl_default_set },
  2585. { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK, 6, light_xyl_default_set },
  2586. { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET, 8, light_xyl_range_set },
  2587. { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK, 8, light_xyl_range_set },
  2588. BLE_MESH_MODEL_OP_END,
  2589. };
  2590. /* Mapping of message handlers for Light LC Server (0x130F) */
  2591. const struct bt_mesh_model_op bt_mesh_light_lc_srv_op[] = {
  2592. { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET, 0, light_lc_get },
  2593. { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET, 1, light_lc_mode_set },
  2594. { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK, 1, light_lc_mode_set },
  2595. { BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET, 0, light_lc_get },
  2596. { BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET, 1, light_lc_om_set },
  2597. { BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK, 1, light_lc_om_set },
  2598. { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET, 0, light_lc_get },
  2599. { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET, 2, light_lc_light_onoff_set },
  2600. { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK, 2, light_lc_light_onoff_set },
  2601. { BLE_MESH_MODEL_OP_SENSOR_STATUS, 3, light_lc_sensor_status },
  2602. BLE_MESH_MODEL_OP_END,
  2603. };
  2604. /* Mapping of message handlers for Light LC Setup Server (0x1310) */
  2605. const struct bt_mesh_model_op bt_mesh_light_lc_setup_srv_op[] = {
  2606. { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET, 2, light_lc_prop_get },
  2607. { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET, 3, light_lc_prop_set },
  2608. { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK, 3, light_lc_prop_set },
  2609. BLE_MESH_MODEL_OP_END,
  2610. };
  2611. static int light_server_init(struct bt_mesh_model *model)
  2612. {
  2613. if (model->user_data == NULL) {
  2614. BT_ERR("Invalid Lighting Server user data, model id 0x%04x", model->id);
  2615. return -EINVAL;
  2616. }
  2617. switch (model->id) {
  2618. case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: {
  2619. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  2620. if (srv->state == NULL) {
  2621. BT_ERR("Invalid Light Lightness State");
  2622. return -EINVAL;
  2623. }
  2624. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2625. bt_mesh_server_alloc_ctx(&srv->actual_transition.timer.work);
  2626. bt_mesh_server_alloc_ctx(&srv->linear_transition.timer.work);
  2627. k_delayed_work_init(&srv->actual_transition.timer, light_lightness_actual_work_handler);
  2628. k_delayed_work_init(&srv->linear_transition.timer, light_lightness_linear_work_handler);
  2629. }
  2630. srv->model = model;
  2631. break;
  2632. }
  2633. case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV: {
  2634. struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
  2635. if (srv->state == NULL) {
  2636. BT_ERR("Invalid Light Lightness State");
  2637. return -EINVAL;
  2638. }
  2639. srv->model = model;
  2640. break;
  2641. }
  2642. case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
  2643. struct bt_mesh_light_ctl_srv *srv = model->user_data;
  2644. if (srv->state == NULL) {
  2645. BT_ERR("Invalid Light CTL State");
  2646. return -EINVAL;
  2647. }
  2648. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2649. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2650. k_delayed_work_init(&srv->transition.timer, light_ctl_work_handler);
  2651. }
  2652. srv->model = model;
  2653. break;
  2654. }
  2655. case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV: {
  2656. struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
  2657. if (srv->state == NULL) {
  2658. BT_ERR("Invalid Light CTL State");
  2659. return -EINVAL;
  2660. }
  2661. srv->model = model;
  2662. break;
  2663. }
  2664. case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
  2665. struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
  2666. if (srv->state == NULL) {
  2667. BT_ERR("Invalid Light CTL State");
  2668. return -EINVAL;
  2669. }
  2670. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2671. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2672. k_delayed_work_init(&srv->transition.timer, light_ctl_temp_work_handler);
  2673. }
  2674. srv->model = model;
  2675. break;
  2676. }
  2677. case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: {
  2678. struct bt_mesh_light_hsl_srv *srv = model->user_data;
  2679. if (srv->state == NULL) {
  2680. BT_ERR("Invalid Light HSL State");
  2681. return -EINVAL;
  2682. }
  2683. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2684. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2685. k_delayed_work_init(&srv->transition.timer, light_hsl_work_handler);
  2686. }
  2687. srv->model = model;
  2688. break;
  2689. }
  2690. case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV: {
  2691. struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
  2692. if (srv->state == NULL) {
  2693. BT_ERR("Invalid Light HSL State");
  2694. return -EINVAL;
  2695. }
  2696. srv->model = model;
  2697. break;
  2698. }
  2699. case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: {
  2700. struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
  2701. if (srv->state == NULL) {
  2702. BT_ERR("Invalid Light HSL State");
  2703. return -EINVAL;
  2704. }
  2705. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2706. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2707. k_delayed_work_init(&srv->transition.timer, light_hsl_hue_work_handler);
  2708. }
  2709. srv->model = model;
  2710. break;
  2711. }
  2712. case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: {
  2713. struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
  2714. if (srv->state == NULL) {
  2715. BT_ERR("Invalid Light HSL State");
  2716. return -EINVAL;
  2717. }
  2718. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2719. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2720. k_delayed_work_init(&srv->transition.timer, light_hsl_sat_work_handler);
  2721. }
  2722. srv->model = model;
  2723. break;
  2724. }
  2725. case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: {
  2726. struct bt_mesh_light_xyl_srv *srv = model->user_data;
  2727. if (srv->state == NULL) {
  2728. BT_ERR("Invalid Light xyL State");
  2729. return -EINVAL;
  2730. }
  2731. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2732. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2733. k_delayed_work_init(&srv->transition.timer, light_xyl_work_handler);
  2734. }
  2735. srv->model = model;
  2736. break;
  2737. }
  2738. case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV: {
  2739. struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
  2740. if (srv->state == NULL) {
  2741. BT_ERR("Invalid Light xyL State");
  2742. return -EINVAL;
  2743. }
  2744. srv->model = model;
  2745. break;
  2746. }
  2747. case BLE_MESH_MODEL_ID_LIGHT_LC_SRV: {
  2748. struct bt_mesh_light_lc_srv *srv = model->user_data;
  2749. if (srv->lc == NULL) {
  2750. BT_ERR("Invalid Light LC State");
  2751. return -EINVAL;
  2752. }
  2753. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2754. bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
  2755. k_delayed_work_init(&srv->transition.timer, light_lc_work_handler);
  2756. }
  2757. srv->model = model;
  2758. break;
  2759. }
  2760. case BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV: {
  2761. struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
  2762. if (srv->lc == NULL) {
  2763. BT_ERR("Invalid Light LC State");
  2764. return -EINVAL;
  2765. }
  2766. srv->model = model;
  2767. break;
  2768. }
  2769. default:
  2770. BT_WARN("Unknown Light Server, model id 0x%04x", model->id);
  2771. return -EINVAL;
  2772. }
  2773. bt_mesh_light_server_mutex_new();
  2774. return 0;
  2775. }
  2776. static int light_lightness_srv_init(struct bt_mesh_model *model)
  2777. {
  2778. if (model->pub == NULL) {
  2779. BT_ERR("Light Lightness Server has no publication support");
  2780. return -EINVAL;
  2781. }
  2782. /* When this model is present on an Element, the corresponding Light Lightness
  2783. * Setup Server model shall also be present.
  2784. */
  2785. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  2786. if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) == NULL) {
  2787. BT_WARN("Light Lightness Setup Server not present");
  2788. /* Just give a warning here, continue with the initialization */
  2789. }
  2790. return light_server_init(model);
  2791. }
  2792. static int light_lightness_setup_srv_init(struct bt_mesh_model *model)
  2793. {
  2794. return light_server_init(model);
  2795. }
  2796. static int light_ctl_srv_init(struct bt_mesh_model *model)
  2797. {
  2798. if (model->pub == NULL) {
  2799. BT_ERR("Light CTL Server has no publication support");
  2800. return -EINVAL;
  2801. }
  2802. /**
  2803. * When this model is present on an Element, the corresponding Light CTL
  2804. * Temperature Server model and the corresponding Light CTL Setup Server
  2805. * model shall also be present.
  2806. * The model requires two elements: the main element and the Temperature
  2807. * element. The Temperature element contains the corresponding Light CTL
  2808. * Temperature Server model.
  2809. */
  2810. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  2811. if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) == NULL) {
  2812. BT_WARN("Light CTL Setup Server not present");
  2813. /* Just give a warning here, continue with the initialization */
  2814. }
  2815. if (bt_mesh_elem_count() < 2) {
  2816. BT_WARN("Light CTL Server requires two elements");
  2817. /* Just give a warning here, continue with the initialization */
  2818. }
  2819. return light_server_init(model);
  2820. }
  2821. static int light_ctl_setup_srv_init(struct bt_mesh_model *model)
  2822. {
  2823. return light_server_init(model);
  2824. }
  2825. static int light_ctl_temp_srv_init(struct bt_mesh_model *model)
  2826. {
  2827. if (model->pub == NULL) {
  2828. BT_ERR("Light CTL Temperature Server has no publication support");
  2829. return -EINVAL;
  2830. }
  2831. return light_server_init(model);
  2832. }
  2833. static int light_hsl_srv_init(struct bt_mesh_model *model)
  2834. {
  2835. if (model->pub == NULL) {
  2836. BT_ERR("Light HSL Server has no publication support");
  2837. return -EINVAL;
  2838. }
  2839. /**
  2840. * When this model is present on an Element, the corresponding Light HSL Hue
  2841. * Server model and the corresponding Light HSL Saturation Server model and
  2842. * the corresponding Light HSL Setup Server model shall also be present.
  2843. * The model requires three elements: the main element and the Hue element
  2844. * and the Saturation element. The Hue element contains the corresponding
  2845. * Light HSL Hue Server model, and the Saturation element contains the corr-
  2846. * esponding Light HSL Saturation Server model.
  2847. */
  2848. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  2849. if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) == NULL) {
  2850. BT_WARN("Light HSL Setup Server not present");
  2851. /* Just give a warning here, continue with the initialization */
  2852. }
  2853. if (bt_mesh_elem_count() < 3) {
  2854. BT_WARN("Light HSL Server requires three elements");
  2855. /* Just give a warning here, continue with the initialization */
  2856. }
  2857. return light_server_init(model);
  2858. }
  2859. static int light_hsl_setup_srv_init(struct bt_mesh_model *model)
  2860. {
  2861. return light_server_init(model);
  2862. }
  2863. static int light_hsl_hue_srv_init(struct bt_mesh_model *model)
  2864. {
  2865. if (model->pub == NULL) {
  2866. BT_ERR("Light HSL Hue Server has no publication support");
  2867. return -EINVAL;
  2868. }
  2869. return light_server_init(model);
  2870. }
  2871. static int light_hsl_sat_srv_init(struct bt_mesh_model *model)
  2872. {
  2873. if (model->pub == NULL) {
  2874. BT_ERR("Light HSL Saturation Server has no publication support");
  2875. return -EINVAL;
  2876. }
  2877. return light_server_init(model);
  2878. }
  2879. static int light_xyl_srv_init(struct bt_mesh_model *model)
  2880. {
  2881. if (model->pub == NULL) {
  2882. BT_ERR("Light xyL Server has no publication support");
  2883. return -EINVAL;
  2884. }
  2885. /**
  2886. * When this model is present on an Element, the corresponding Light xyL
  2887. * Setup Server model shall also be present.
  2888. */
  2889. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  2890. if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV) == NULL) {
  2891. BT_WARN("Light xyL Setup Server not present");
  2892. /* Just give a warning here, continue with the initialization */
  2893. }
  2894. return light_server_init(model);
  2895. }
  2896. static int light_xyl_setup_srv_init(struct bt_mesh_model *model)
  2897. {
  2898. return light_server_init(model);
  2899. }
  2900. static int light_lc_srv_init(struct bt_mesh_model *model)
  2901. {
  2902. if (model->pub == NULL) {
  2903. BT_ERR("Light LC Server has no publication support");
  2904. return -EINVAL;
  2905. }
  2906. return light_server_init(model);
  2907. }
  2908. static int light_lc_setup_srv_init(struct bt_mesh_model *model)
  2909. {
  2910. if (model->pub == NULL) {
  2911. BT_ERR("Light LC Setup Server has no publication support");
  2912. return -EINVAL;
  2913. }
  2914. /**
  2915. * When this model is present on an Element, the corresponding Light LC
  2916. * Setup Server model shall also be present.
  2917. */
  2918. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  2919. if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV) == NULL) {
  2920. BT_WARN("Light LC Setup Server not present");
  2921. /* Just give a warning here, continue with the initialization */
  2922. }
  2923. return light_server_init(model);
  2924. }
  2925. #if CONFIG_BLE_MESH_DEINIT
  2926. static int light_server_deinit(struct bt_mesh_model *model)
  2927. {
  2928. if (model->user_data == NULL) {
  2929. BT_ERR("Invalid Lighting Server user data, model id 0x%04x", model->id);
  2930. return -EINVAL;
  2931. }
  2932. switch (model->id) {
  2933. case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: {
  2934. struct bt_mesh_light_lightness_srv *srv = model->user_data;
  2935. if (srv->state == NULL) {
  2936. BT_ERR("Invalid Light Lightness State");
  2937. return -EINVAL;
  2938. }
  2939. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2940. bt_mesh_server_free_ctx(&srv->actual_transition.timer.work);
  2941. bt_mesh_server_free_ctx(&srv->linear_transition.timer.work);
  2942. k_delayed_work_free(&srv->actual_transition.timer);
  2943. k_delayed_work_free(&srv->linear_transition.timer);
  2944. }
  2945. break;
  2946. }
  2947. case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
  2948. struct bt_mesh_light_ctl_srv *srv = model->user_data;
  2949. if (srv->state == NULL) {
  2950. BT_ERR("Invalid Light CTL State");
  2951. return -EINVAL;
  2952. }
  2953. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2954. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  2955. k_delayed_work_free(&srv->transition.timer);
  2956. }
  2957. break;
  2958. }
  2959. case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
  2960. struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
  2961. if (srv->state == NULL) {
  2962. BT_ERR("Invalid Light CTL State");
  2963. return -EINVAL;
  2964. }
  2965. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2966. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  2967. k_delayed_work_free(&srv->transition.timer);
  2968. }
  2969. break;
  2970. }
  2971. case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: {
  2972. struct bt_mesh_light_hsl_srv *srv = model->user_data;
  2973. if (srv->state == NULL) {
  2974. BT_ERR("Invalid Light HSL State");
  2975. return -EINVAL;
  2976. }
  2977. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2978. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  2979. k_delayed_work_free(&srv->transition.timer);
  2980. }
  2981. break;
  2982. }
  2983. case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: {
  2984. struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
  2985. if (srv->state == NULL) {
  2986. BT_ERR("Invalid Light HSL State");
  2987. return -EINVAL;
  2988. }
  2989. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  2990. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  2991. k_delayed_work_free(&srv->transition.timer);
  2992. }
  2993. break;
  2994. }
  2995. case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: {
  2996. struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
  2997. if (srv->state == NULL) {
  2998. BT_ERR("Invalid Light HSL State");
  2999. return -EINVAL;
  3000. }
  3001. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  3002. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  3003. k_delayed_work_free(&srv->transition.timer);
  3004. }
  3005. break;
  3006. }
  3007. case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: {
  3008. struct bt_mesh_light_xyl_srv *srv = model->user_data;
  3009. if (srv->state == NULL) {
  3010. BT_ERR("Invalid Light xyL State");
  3011. return -EINVAL;
  3012. }
  3013. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  3014. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  3015. k_delayed_work_free(&srv->transition.timer);
  3016. }
  3017. break;
  3018. }
  3019. case BLE_MESH_MODEL_ID_LIGHT_LC_SRV: {
  3020. struct bt_mesh_light_lc_srv *srv = model->user_data;
  3021. if (srv->lc == NULL) {
  3022. BT_ERR("Invalid Light LC State");
  3023. return -EINVAL;
  3024. }
  3025. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
  3026. bt_mesh_server_free_ctx(&srv->transition.timer.work);
  3027. k_delayed_work_free(&srv->transition.timer);
  3028. }
  3029. break;
  3030. }
  3031. case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV:
  3032. case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV:
  3033. case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV:
  3034. case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV:
  3035. case BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV:
  3036. break;
  3037. default:
  3038. BT_WARN("Unknown Light Server, model id 0x%04x", model->id);
  3039. return -EINVAL;
  3040. }
  3041. bt_mesh_light_server_mutex_free();
  3042. return 0;
  3043. }
  3044. static int light_lightness_srv_deinit(struct bt_mesh_model *model)
  3045. {
  3046. if (model->pub == NULL) {
  3047. BT_ERR("Light Lightness Server has no publication support");
  3048. return -EINVAL;
  3049. }
  3050. return light_server_deinit(model);
  3051. }
  3052. static int light_lightness_setup_srv_deinit(struct bt_mesh_model *model)
  3053. {
  3054. return light_server_deinit(model);
  3055. }
  3056. static int light_ctl_srv_deinit(struct bt_mesh_model *model)
  3057. {
  3058. if (model->pub == NULL) {
  3059. BT_ERR("Light CTL Server has no publication support");
  3060. return -EINVAL;
  3061. }
  3062. return light_server_deinit(model);
  3063. }
  3064. static int light_ctl_setup_srv_deinit(struct bt_mesh_model *model)
  3065. {
  3066. return light_server_deinit(model);
  3067. }
  3068. static int light_ctl_temp_srv_deinit(struct bt_mesh_model *model)
  3069. {
  3070. if (model->pub == NULL) {
  3071. BT_ERR("Light CTL Temperature Server has no publication support");
  3072. return -EINVAL;
  3073. }
  3074. return light_server_deinit(model);
  3075. }
  3076. static int light_hsl_srv_deinit(struct bt_mesh_model *model)
  3077. {
  3078. if (model->pub == NULL) {
  3079. BT_ERR("Light HSL Server has no publication support");
  3080. return -EINVAL;
  3081. }
  3082. return light_server_deinit(model);
  3083. }
  3084. static int light_hsl_setup_srv_deinit(struct bt_mesh_model *model)
  3085. {
  3086. return light_server_deinit(model);
  3087. }
  3088. static int light_hsl_hue_srv_deinit(struct bt_mesh_model *model)
  3089. {
  3090. if (model->pub == NULL) {
  3091. BT_ERR("Light HSL Hue Server has no publication support");
  3092. return -EINVAL;
  3093. }
  3094. return light_server_deinit(model);
  3095. }
  3096. static int light_hsl_sat_srv_deinit(struct bt_mesh_model *model)
  3097. {
  3098. if (model->pub == NULL) {
  3099. BT_ERR("Light HSL Saturation Server has no publication support");
  3100. return -EINVAL;
  3101. }
  3102. return light_server_deinit(model);
  3103. }
  3104. static int light_xyl_srv_deinit(struct bt_mesh_model *model)
  3105. {
  3106. if (model->pub == NULL) {
  3107. BT_ERR("Light xyL Server has no publication support");
  3108. return -EINVAL;
  3109. }
  3110. return light_server_deinit(model);
  3111. }
  3112. static int light_xyl_setup_srv_deinit(struct bt_mesh_model *model)
  3113. {
  3114. return light_server_deinit(model);
  3115. }
  3116. static int light_lc_srv_deinit(struct bt_mesh_model *model)
  3117. {
  3118. if (model->pub == NULL) {
  3119. BT_ERR("Light LC Server has no publication support");
  3120. return -EINVAL;
  3121. }
  3122. return light_server_deinit(model);
  3123. }
  3124. static int light_lc_setup_srv_deinit(struct bt_mesh_model *model)
  3125. {
  3126. if (model->pub == NULL) {
  3127. BT_ERR("Light LC Setup Server has no publication support");
  3128. return -EINVAL;
  3129. }
  3130. return light_server_deinit(model);
  3131. }
  3132. #endif /* CONFIG_BLE_MESH_DEINIT */
  3133. const struct bt_mesh_model_cb bt_mesh_light_lightness_srv_cb = {
  3134. .init = light_lightness_srv_init,
  3135. #if CONFIG_BLE_MESH_DEINIT
  3136. .deinit = light_lightness_srv_deinit,
  3137. #endif /* CONFIG_BLE_MESH_DEINIT */
  3138. };
  3139. const struct bt_mesh_model_cb bt_mesh_light_lightness_setup_srv_cb = {
  3140. .init = light_lightness_setup_srv_init,
  3141. #if CONFIG_BLE_MESH_DEINIT
  3142. .deinit = light_lightness_setup_srv_deinit,
  3143. #endif /* CONFIG_BLE_MESH_DEINIT */
  3144. };
  3145. const struct bt_mesh_model_cb bt_mesh_light_ctl_srv_cb = {
  3146. .init = light_ctl_srv_init,
  3147. #if CONFIG_BLE_MESH_DEINIT
  3148. .deinit = light_ctl_srv_deinit,
  3149. #endif /* CONFIG_BLE_MESH_DEINIT */
  3150. };
  3151. const struct bt_mesh_model_cb bt_mesh_light_ctl_setup_srv_cb = {
  3152. .init = light_ctl_setup_srv_init,
  3153. #if CONFIG_BLE_MESH_DEINIT
  3154. .deinit = light_ctl_setup_srv_deinit,
  3155. #endif /* CONFIG_BLE_MESH_DEINIT */
  3156. };
  3157. const struct bt_mesh_model_cb bt_mesh_light_ctl_temp_srv_cb = {
  3158. .init = light_ctl_temp_srv_init,
  3159. #if CONFIG_BLE_MESH_DEINIT
  3160. .deinit = light_ctl_temp_srv_deinit,
  3161. #endif /* CONFIG_BLE_MESH_DEINIT */
  3162. };
  3163. const struct bt_mesh_model_cb bt_mesh_light_hsl_srv_cb = {
  3164. .init = light_hsl_srv_init,
  3165. #if CONFIG_BLE_MESH_DEINIT
  3166. .deinit = light_hsl_srv_deinit,
  3167. #endif /* CONFIG_BLE_MESH_DEINIT */
  3168. };
  3169. const struct bt_mesh_model_cb bt_mesh_light_hsl_setup_srv_cb = {
  3170. .init = light_hsl_setup_srv_init,
  3171. #if CONFIG_BLE_MESH_DEINIT
  3172. .deinit = light_hsl_setup_srv_deinit,
  3173. #endif /* CONFIG_BLE_MESH_DEINIT */
  3174. };
  3175. const struct bt_mesh_model_cb bt_mesh_light_hsl_hue_srv_cb = {
  3176. .init = light_hsl_hue_srv_init,
  3177. #if CONFIG_BLE_MESH_DEINIT
  3178. .deinit = light_hsl_hue_srv_deinit,
  3179. #endif /* CONFIG_BLE_MESH_DEINIT */
  3180. };
  3181. const struct bt_mesh_model_cb bt_mesh_light_hsl_sat_srv_cb = {
  3182. .init = light_hsl_sat_srv_init,
  3183. #if CONFIG_BLE_MESH_DEINIT
  3184. .deinit = light_hsl_sat_srv_deinit,
  3185. #endif /* CONFIG_BLE_MESH_DEINIT */
  3186. };
  3187. const struct bt_mesh_model_cb bt_mesh_light_xyl_srv_cb = {
  3188. .init = light_xyl_srv_init,
  3189. #if CONFIG_BLE_MESH_DEINIT
  3190. .deinit = light_xyl_srv_deinit,
  3191. #endif /* CONFIG_BLE_MESH_DEINIT */
  3192. };
  3193. const struct bt_mesh_model_cb bt_mesh_light_xyl_setup_srv_cb = {
  3194. .init = light_xyl_setup_srv_init,
  3195. #if CONFIG_BLE_MESH_DEINIT
  3196. .deinit = light_xyl_setup_srv_deinit,
  3197. #endif /* CONFIG_BLE_MESH_DEINIT */
  3198. };
  3199. const struct bt_mesh_model_cb bt_mesh_light_lc_srv_cb = {
  3200. .init = light_lc_srv_init,
  3201. #if CONFIG_BLE_MESH_DEINIT
  3202. .deinit = light_lc_srv_deinit,
  3203. #endif /* CONFIG_BLE_MESH_DEINIT */
  3204. };
  3205. const struct bt_mesh_model_cb bt_mesh_light_lc_setup_srv_cb = {
  3206. .init = light_lc_setup_srv_init,
  3207. #if CONFIG_BLE_MESH_DEINIT
  3208. .deinit = light_lc_setup_srv_deinit,
  3209. #endif /* CONFIG_BLE_MESH_DEINIT */
  3210. };
  3211. #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */