lighting_server.c 132 KB

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