| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553 |
- /* Bluetooth: Mesh Lighting Server Models
- *
- * Copyright (c) 2018 Vikrant More
- * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #include <errno.h>
- #include "access.h"
- #include "transport.h"
- #include "model_opcode.h"
- #include "state_transition.h"
- #include "device_property.h"
- #include "btc_ble_mesh_lighting_model.h"
- static bt_mesh_mutex_t light_server_lock;
- static void bt_mesh_light_server_mutex_new(void)
- {
- if (!light_server_lock.mutex) {
- bt_mesh_mutex_create(&light_server_lock);
- }
- }
- static void bt_mesh_light_server_mutex_free(void)
- {
- bt_mesh_mutex_free(&light_server_lock);
- }
- void bt_mesh_light_server_lock(void)
- {
- bt_mesh_mutex_lock(&light_server_lock);
- }
- void bt_mesh_light_server_unlock(void)
- {
- bt_mesh_mutex_unlock(&light_server_lock);
- }
- /* message handlers (Start) */
- /* Light Lightness Server/Setup Server message handlers */
- static void send_light_lightness_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- bool publish, u16_t opcode)
- {
- struct net_buf_simple *msg = NULL;
- u8_t length = 2 + 5;
- if (ctx == NULL && publish == false) {
- BT_ERR("%s, Invalid parameter", __func__);
- return;
- }
- if (publish == false) {
- msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
- if (msg == NULL) {
- BT_ERR("%s, Failed to allocate memory", __func__);
- return;
- }
- } else {
- msg = bt_mesh_server_get_pub_msg(model, length);
- if (msg == NULL) {
- return;
- }
- }
- bt_mesh_model_msg_init(msg, opcode);
- switch (opcode) {
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS: {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_actual);
- if (srv->actual_transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->actual_transition);
- net_buf_simple_add_le16(msg, srv->state->target_lightness_actual);
- net_buf_simple_add_u8(msg, srv->actual_transition.remain_time);
- }
- break;
- }
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS: {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_linear);
- if (srv->linear_transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->linear_transition);
- net_buf_simple_add_le16(msg, srv->state->target_lightness_linear);
- net_buf_simple_add_u8(msg, srv->linear_transition.remain_time);
- }
- break;
- }
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS: {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_last);
- break;
- }
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS:
- if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_default);
- } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) {
- struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_default);
- }
- break;
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS:
- if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- net_buf_simple_add_u8(msg, srv->state->status_code);
- net_buf_simple_add_le16(msg, srv->state->lightness_range_min);
- net_buf_simple_add_le16(msg, srv->state->lightness_range_max);
- } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) {
- struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
- net_buf_simple_add_u8(msg, srv->state->status_code);
- net_buf_simple_add_le16(msg, srv->state->lightness_range_min);
- net_buf_simple_add_le16(msg, srv->state->lightness_range_max);
- }
- break;
- default:
- BT_WARN("%s, Unknown Light Lightness status opcode 0x%04x", __func__, opcode);
- if (publish == false) {
- bt_mesh_free_buf(msg);
- }
- return;
- }
- if (publish == false) {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
- bt_mesh_free_buf(msg);
- } else {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
- }
- return;
- }
- static void light_lightness_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- u16_t opcode = 0U;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
- return;
- }
- switch (ctx->recv_op) {
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS;
- break;
- default:
- BT_WARN("%s, Unknown Light Lightness Get opcode 0x%04x", __func__, ctx->recv_op);
- return;
- }
- send_light_lightness_status(model, ctx, false, opcode);
- return;
- }
- void light_lightness_publish(struct bt_mesh_model *model, u16_t opcode)
- {
- if (model->user_data == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- switch (model->id) {
- case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light Lightness Server state", __func__);
- return;
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV: {
- struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light Lightness Setup Server state", __func__);
- return;
- }
- break;
- }
- default:
- BT_ERR("%s, Invalid Light Lightness Server Model 0x%04x", __func__, model->id);
- return;
- }
- send_light_lightness_status(model, NULL, true, opcode);
- return;
- }
- static void light_lightness_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- u8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- u16_t actual = 0U;
- s64_t now = 0;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- actual = net_buf_simple_pull_le16(buf);
- tid = net_buf_simple_pull_u8(buf);
- if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .lightness_set.op_en = optional,
- .lightness_set.lightness = actual,
- .lightness_set.tid = tid,
- .lightness_set.trans_time = trans_time,
- .lightness_set.delay = delay,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
- /* In this condition, no event will be callback to application layer */
- return;
- }
- bt_mesh_light_server_lock();
- bt_mesh_server_stop_transition(&srv->actual_transition);
- bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
- if (actual) {
- if (srv->state->lightness_range_min && actual < srv->state->lightness_range_min) {
- actual = srv->state->lightness_range_min;
- } else if (srv->state->lightness_range_max && actual > srv->state->lightness_range_max) {
- actual = srv->state->lightness_range_max;
- }
- }
- srv->state->target_lightness_actual = actual;
- /**
- * If the target state is equal to the current state, the transition shall not be
- * started and is considered complete.
- */
- if (srv->state->target_lightness_actual != srv->state->lightness_actual) {
- light_lightness_actual_tt_values(srv, trans_time, delay);
- } else {
- bt_mesh_light_server_state_change_t change = {
- .lightness_set.lightness = srv->state->lightness_actual,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
- bt_mesh_light_server_unlock();
- return;
- }
- /* Copy the ctx of the received message */
- if (srv->actual_transition.timer.work._reserved) {
- memcpy(srv->actual_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->actual_transition.counter == 0U) {
- srv->state->lightness_actual = srv->state->target_lightness_actual;
- /**
- * Whenever the Light Lightness Actual state is changed with a non-transactional
- * message or a completed sequence of transactional messages to a non-zero value,
- * the value of the Light Lightness Last shall be set to the value of the Light
- * Lightness Actual.
- */
- if (srv->state->lightness_actual) {
- srv->state->lightness_last = srv->state->lightness_actual;
- }
- }
- srv->actual_transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->actual_transition);
- return;
- }
- static void light_lightness_linear_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- u8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- u16_t linear = 0U;
- s64_t now = 0;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- linear = net_buf_simple_pull_le16(buf);
- tid = net_buf_simple_pull_u8(buf);
- if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .lightness_linear_set.op_en = optional,
- .lightness_linear_set.lightness = linear,
- .lightness_linear_set.tid = tid,
- .lightness_linear_set.trans_time = trans_time,
- .lightness_linear_set.delay = delay,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
- /* In this condition, no event will be callback to application layer */
- return;
- }
- bt_mesh_light_server_lock();
- bt_mesh_server_stop_transition(&srv->linear_transition);
- bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
- srv->state->target_lightness_linear = linear;
- /**
- * If the target state is equal to the current state, the transition shall not
- * be started and is considered complete.
- */
- if (srv->state->target_lightness_linear != srv->state->lightness_linear) {
- light_lightness_linear_tt_values(srv, trans_time, delay);
- } else {
- bt_mesh_light_server_state_change_t change = {
- .lightness_linear_set.lightness = srv->state->lightness_actual,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
- bt_mesh_light_server_unlock();
- return;
- }
- /* Copy the ctx of the received message */
- if (srv->linear_transition.timer.work._reserved) {
- memcpy(srv->linear_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->linear_transition.counter == 0U) {
- srv->state->lightness_linear = srv->state->target_lightness_linear;
- }
- srv->linear_transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->linear_transition);
- return;
- }
- static void light_lightness_default_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
- u16_t lightness = 0U;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- lightness = net_buf_simple_pull_le16(buf);
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .lightness_default_set.lightness = lightness,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (srv->state->lightness_default != lightness) {
- srv->state->lightness_default = lightness;
- bt_mesh_light_server_state_change_t change = {
- .lightness_default_set.lightness = lightness,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- }
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS);
- return;
- }
- static void light_lightness_range_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
- u16_t range_min = 0U, range_max = 0U;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- range_min = net_buf_simple_pull_le16(buf);
- range_max = net_buf_simple_pull_le16(buf);
- if (range_min > range_max) {
- BT_ERR("%s, Range Min 0x%04x is greater than Range Max 0x%04x",
- __func__, range_min, range_max);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .lightness_range_set.range_min = range_min,
- .lightness_range_set.range_max = range_max,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- /**
- * When a Light Lightness Setup Server receives a Light Lightness Range Set
- * message or a Light Lightness Range Set Unacknowledged message with values
- * that cannot be accepted, it shall set the status of the operation to a
- * value representing the reason why the values cannot be accepted.
- *
- * TODO: 0x0000 for Light Range Min/Max is prohibited, but BQB test case
- * MMDL/SR/LLNS/BI-01-C requires 'SUCCESS' when it sends a set message with
- * Light Range Min set to 0x0000.
- */
- #if 0
- srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
- #else
- if (range_min == 0x0000) {
- srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN;
- } else if (range_max == 0x0000) {
- srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX;
- } else {
- srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
- }
- #endif
- if (range_min && srv->state->lightness_range_min != range_min) {
- srv->state->lightness_range_min = range_min;
- }
- if (range_max && srv->state->lightness_range_max != range_max) {
- srv->state->lightness_range_max = range_max;
- }
- bt_mesh_light_server_state_change_t change = {
- .lightness_range_set.range_min = srv->state->lightness_range_min,
- .lightness_range_set.range_max = srv->state->lightness_range_max,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS);
- return;
- }
- /* Light CTL Server/Temperature Server/Setup Server message handlers */
- static void send_light_ctl_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- bool publish, u16_t opcode)
- {
- struct net_buf_simple *msg = NULL;
- u8_t length = 2 + 9;
- if (ctx == NULL && publish == false) {
- BT_ERR("%s, Invalid parameter", __func__);
- return;
- }
- if (publish == false) {
- msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
- if (msg == NULL) {
- BT_ERR("%s, Failed to allocate memory", __func__);
- return;
- }
- } else {
- msg = bt_mesh_server_get_pub_msg(model, length);
- if (msg == NULL) {
- return;
- }
- }
- bt_mesh_model_msg_init(msg, opcode);
- switch (opcode) {
- case BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS: {
- struct bt_mesh_light_ctl_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness);
- net_buf_simple_add_le16(msg, srv->state->temperature);
- if (srv->transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->transition);
- net_buf_simple_add_le16(msg, srv->state->target_lightness);
- net_buf_simple_add_le16(msg, srv->state->target_temperature);
- net_buf_simple_add_u8(msg, srv->transition.remain_time);
- }
- break;
- }
- case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS:
- if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SRV) {
- struct bt_mesh_light_ctl_srv *srv = model->user_data;
- net_buf_simple_add_u8(msg, srv->state->status_code);
- net_buf_simple_add_le16(msg, srv->state->temperature_range_min);
- net_buf_simple_add_le16(msg, srv->state->temperature_range_max);
- } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) {
- struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
- net_buf_simple_add_u8(msg, srv->state->status_code);
- net_buf_simple_add_le16(msg, srv->state->temperature_range_min);
- net_buf_simple_add_le16(msg, srv->state->temperature_range_max);
- }
- break;
- case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS: {
- if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SRV) {
- struct bt_mesh_light_ctl_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_default);
- net_buf_simple_add_le16(msg, srv->state->temperature_default);
- net_buf_simple_add_le16(msg, srv->state->delta_uv_default);
- } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) {
- struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_default);
- net_buf_simple_add_le16(msg, srv->state->temperature_default);
- net_buf_simple_add_le16(msg, srv->state->delta_uv_default);
- }
- break;
- }
- case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS: {
- struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->temperature);
- net_buf_simple_add_le16(msg, srv->state->delta_uv);
- if (srv->transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->transition);
- net_buf_simple_add_le16(msg, srv->state->target_temperature);
- net_buf_simple_add_le16(msg, srv->state->target_delta_uv);
- net_buf_simple_add_u8(msg, srv->transition.remain_time);
- }
- break;
- }
- default:
- BT_WARN("%s, Unknown Light CTL status opcode 0x%04x", __func__, opcode);
- if (publish == false) {
- bt_mesh_free_buf(msg);
- }
- return;
- }
- if (publish == false) {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
- bt_mesh_free_buf(msg);
- } else {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
- }
- return;
- }
- static void light_ctl_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL;
- u16_t opcode = 0U;
- if (model->user_data == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- switch (model->id) {
- case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
- struct bt_mesh_light_ctl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light CTL Server state", __func__);
- return;
- }
- rsp_ctrl = &srv->rsp_ctrl;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
- struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light CTL Temperature Server state", __func__);
- return;
- }
- rsp_ctrl = &srv->rsp_ctrl;
- break;
- }
- default:
- BT_ERR("%s, Invalid Light CTL Server Model 0x%04x", __func__, model->id);
- return;
- }
- /* Callback the received message to the application layer */
- if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
- return;
- }
- switch (ctx->recv_op) {
- case BLE_MESH_MODEL_OP_LIGHT_CTL_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS;
- break;
- default:
- BT_WARN("%s, Unknown Light CTL Get opcode 0x%04x", __func__, ctx->recv_op);
- return;
- }
- send_light_ctl_status(model, ctx, false, opcode);
- return;
- }
- void light_ctl_publish(struct bt_mesh_model *model, u16_t opcode)
- {
- if (model->user_data == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- switch (model->id) {
- case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
- struct bt_mesh_light_ctl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light CTL Server state", __func__);
- return;
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
- struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light CTL Temperature Server state", __func__);
- return;
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV: {
- struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light CTL Setup Server state", __func__);
- return;
- }
- break;
- }
- default:
- BT_ERR("%s, Invalid Light CTL Server Model 0x%04x", __func__, model->id);
- return;
- }
- send_light_ctl_status(model, NULL, true, opcode);
- return;
- }
- static void light_ctl_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_ctl_srv *srv = model->user_data;
- u16_t lightness = 0U, temperature = 0U;
- u8_t tid = 0U, trans_time = 0U, delay = 0U;
- s16_t delta_uv = 0;
- bool optional = false;
- s64_t now = 0;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- lightness = net_buf_simple_pull_le16(buf);
- temperature = net_buf_simple_pull_le16(buf);
- delta_uv = (s16_t) net_buf_simple_pull_le16(buf);
- tid = net_buf_simple_pull_u8(buf);
- if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) {
- BT_ERR("%s, Invalid temperature 0x%04x", __func__, temperature);
- return;
- }
- if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .ctl_set.op_en = optional,
- .ctl_set.lightness = lightness,
- .ctl_set.temperature = temperature,
- .ctl_set.delta_uv = delta_uv,
- .ctl_set.tid = tid,
- .ctl_set.trans_time = trans_time,
- .ctl_set.delay = delay,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
- /* In this condition, no event will be callback to application layer */
- return;
- }
- bt_mesh_light_server_lock();
- bt_mesh_server_stop_transition(&srv->transition);
- bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
- srv->state->target_lightness = lightness;
- if (srv->state->temperature_range_min &&
- srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN &&
- temperature < srv->state->temperature_range_min) {
- temperature = srv->state->temperature_range_min;
- } else if (srv->state->temperature_range_max &&
- srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN &&
- temperature > srv->state->temperature_range_max) {
- temperature = srv->state->temperature_range_max;
- }
- srv->state->target_temperature = temperature;
- srv->state->target_delta_uv = delta_uv;
- if (srv->state->target_lightness != srv->state->lightness ||
- srv->state->target_temperature != srv->state->temperature ||
- srv->state->target_delta_uv != srv->state->delta_uv) {
- light_ctl_tt_values(srv, trans_time, delay);
- } else {
- bt_mesh_light_server_state_change_t change = {
- .ctl_set.lightness = srv->state->lightness,
- .ctl_set.temperature = srv->state->temperature,
- .ctl_set.delta_uv = srv->state->delta_uv,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
- bt_mesh_light_server_unlock();
- return;
- }
- /* Copy the ctx of the received message */
- if (srv->transition.timer.work._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->lightness = srv->state->target_lightness;
- srv->state->temperature = srv->state->target_temperature;
- srv->state->delta_uv = srv->state->target_delta_uv;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_ctl_default_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
- u16_t lightness = 0U, temperature = 0U;
- s16_t delta_uv = 0;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- lightness = net_buf_simple_pull_le16(buf);
- temperature = net_buf_simple_pull_le16(buf);
- delta_uv = (s16_t) net_buf_simple_pull_le16(buf);
- if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) {
- BT_ERR("%s, Invalid temperature 0x%04x", __func__, temperature);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .ctl_default_set.lightness = lightness,
- .ctl_default_set.temperature = temperature,
- .ctl_default_set.delta_uv = delta_uv,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (srv->state->temperature_range_min &&
- srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN &&
- temperature < srv->state->temperature_range_min) {
- temperature = srv->state->temperature_range_min;
- } else if (srv->state->temperature_range_max &&
- srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN &&
- temperature > srv->state->temperature_range_max) {
- temperature = srv->state->temperature_range_max;
- }
- srv->state->lightness_default = lightness;
- srv->state->temperature_default = temperature;
- srv->state->delta_uv_default = delta_uv;
- bt_mesh_light_server_state_change_t change = {
- .ctl_default_set.lightness = srv->state->lightness_default,
- .ctl_default_set.temperature = srv->state->temperature_default,
- .ctl_default_set.delta_uv = srv->state->delta_uv_default,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS);
- return;
- }
- static void light_ctl_temp_range_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
- u16_t min = 0U, max = 0U;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- min = net_buf_simple_pull_le16(buf);
- max = net_buf_simple_pull_le16(buf);
- /* This is as per 6.1.3.1 in Mesh Model Specification */
- if (min > max ||
- min < BLE_MESH_TEMPERATURE_MIN || (min != BLE_MESH_TEMPERATURE_UNKNOWN && min > BLE_MESH_TEMPERATURE_MAX) ||
- max < BLE_MESH_TEMPERATURE_MIN || (max != BLE_MESH_TEMPERATURE_UNKNOWN && max > BLE_MESH_TEMPERATURE_MAX)) {
- BT_ERR("%s, Invalid parameter, range Min 0x%04x, range max 0x%04x",
- __func__, min, max);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .ctl_temp_range_set.range_min = min,
- .ctl_temp_range_set.range_max = max,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (min == BLE_MESH_TEMPERATURE_UNKNOWN) {
- srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN;
- } else if (max == BLE_MESH_TEMPERATURE_UNKNOWN ) {
- srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX;
- } else {
- srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
- }
- if (min != BLE_MESH_TEMPERATURE_UNKNOWN && srv->state->temperature_range_min != min) {
- srv->state->temperature_range_min = min;
- }
- if (max != BLE_MESH_TEMPERATURE_UNKNOWN && srv->state->temperature_range_max != max) {
- srv->state->temperature_range_max = max;
- }
- bt_mesh_light_server_state_change_t change = {
- .ctl_temp_range_set.range_min = srv->state->temperature_range_min,
- .ctl_temp_range_set.range_max = srv->state->temperature_range_max,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS);
- return;
- }
- static void light_ctl_temp_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
- u8_t tid = 0U, trans_time = 0U, delay = 0U;
- u16_t temperature = 0U;
- s16_t delta_uv = 0;
- bool optional = false;
- s64_t now = 0;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- temperature = net_buf_simple_pull_le16(buf);
- delta_uv = (s16_t) net_buf_simple_pull_le16(buf);
- tid = net_buf_simple_pull_u8(buf);
- if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) {
- BT_ERR("%s, Invalid temperature 0x%04x", __func__, temperature);
- return;
- }
- if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .ctl_temp_set.op_en = optional,
- .ctl_temp_set.temperature = temperature,
- .ctl_temp_set.delta_uv = delta_uv,
- .ctl_temp_set.tid = tid,
- .ctl_temp_set.trans_time = trans_time,
- .ctl_temp_set.delay = delay,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
- /* In this condition, no event will be callback to application layer */
- return;
- }
- bt_mesh_light_server_lock();
- bt_mesh_server_stop_transition(&srv->transition);
- bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
- if (srv->state->temperature_range_min &&
- srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN &&
- temperature < srv->state->temperature_range_min) {
- temperature = srv->state->temperature_range_min;
- } else if (srv->state->temperature_range_max &&
- srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN &&
- temperature > srv->state->temperature_range_max) {
- temperature = srv->state->temperature_range_max;
- }
- srv->state->target_temperature = temperature;
- srv->state->target_delta_uv = delta_uv;
- if (srv->state->target_temperature != srv->state->temperature ||
- srv->state->target_delta_uv != srv->state->delta_uv) {
- light_ctl_temp_tt_values(srv, trans_time, delay);
- } else {
- bt_mesh_light_server_state_change_t change = {
- .ctl_temp_set.temperature = srv->state->temperature,
- .ctl_temp_set.delta_uv = srv->state->delta_uv,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
- bt_mesh_light_server_unlock();
- return;
- }
- /* Copy the ctx of the received message */
- if (srv->transition.timer.work._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->temperature = srv->state->target_temperature;
- srv->state->delta_uv = srv->state->target_delta_uv;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- /* Light HSL Server/Hue Server/Saturation Server/Setup Server message handlers */
- static void send_light_hsl_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- bool publish, u16_t opcode)
- {
- struct net_buf_simple *msg = NULL;
- u8_t length = 2 + 9;
- if (ctx == NULL && publish == false) {
- BT_ERR("%s, Invalid parameter", __func__);
- return;
- }
- if (publish == false) {
- msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
- if (msg == NULL) {
- BT_ERR("%s, Failed to allocate memory", __func__);
- return;
- }
- } else {
- msg = bt_mesh_server_get_pub_msg(model, length);
- if (msg == NULL) {
- return;
- }
- }
- bt_mesh_model_msg_init(msg, opcode);
- switch (opcode) {
- case BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS:
- case BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS: {
- struct bt_mesh_light_hsl_srv *srv = model->user_data;
- if (opcode == BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS) {
- net_buf_simple_add_le16(msg, srv->state->lightness);
- net_buf_simple_add_le16(msg, srv->state->hue);
- net_buf_simple_add_le16(msg, srv->state->saturation);
- if (srv->transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->transition);
- net_buf_simple_add_u8(msg, srv->transition.remain_time);
- }
- } else if (opcode == BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS) {
- net_buf_simple_add_le16(msg, srv->state->target_lightness);
- net_buf_simple_add_le16(msg, srv->state->target_hue);
- net_buf_simple_add_le16(msg, srv->state->target_saturation);
- if (srv->transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->transition);
- net_buf_simple_add_u8(msg, srv->transition.remain_time);
- }
- }
- break;
- }
- case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS:
- if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SRV) {
- struct bt_mesh_light_hsl_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_default);
- net_buf_simple_add_le16(msg, srv->state->hue_default);
- net_buf_simple_add_le16(msg, srv->state->saturation_default);
- } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) {
- struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_default);
- net_buf_simple_add_le16(msg, srv->state->hue_default);
- net_buf_simple_add_le16(msg, srv->state->saturation_default);
- }
- break;
- case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS:
- if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SRV) {
- struct bt_mesh_light_hsl_srv *srv = model->user_data;
- net_buf_simple_add_u8(msg, srv->state->status_code);
- net_buf_simple_add_le16(msg, srv->state->hue_range_min);
- net_buf_simple_add_le16(msg, srv->state->hue_range_max);
- net_buf_simple_add_le16(msg, srv->state->saturation_range_min);
- net_buf_simple_add_le16(msg, srv->state->saturation_range_max);
- } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) {
- struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
- net_buf_simple_add_u8(msg, srv->state->status_code);
- net_buf_simple_add_le16(msg, srv->state->hue_range_min);
- net_buf_simple_add_le16(msg, srv->state->hue_range_max);
- net_buf_simple_add_le16(msg, srv->state->saturation_range_min);
- net_buf_simple_add_le16(msg, srv->state->saturation_range_max);
- }
- break;
- case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS: {
- struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->hue);
- if (srv->transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->transition);
- net_buf_simple_add_le16(msg, srv->state->target_hue);
- net_buf_simple_add_u8(msg, srv->transition.remain_time);
- }
- break;
- }
- case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS: {
- struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->saturation);
- if (srv->transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->transition);
- net_buf_simple_add_le16(msg, srv->state->target_saturation);
- net_buf_simple_add_u8(msg, srv->transition.remain_time);
- }
- break;
- }
- default:
- BT_WARN("%s, Unknown Light HSL status opcode 0x%04x", __func__, opcode);
- if (publish == false) {
- bt_mesh_free_buf(msg);
- }
- return;
- }
- if (publish == false) {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
- bt_mesh_free_buf(msg);
- } else {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
- }
- return;
- }
- static void light_hsl_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL;
- u16_t opcode = 0U;
- if (model->user_data == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- switch (model->id) {
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: {
- struct bt_mesh_light_hsl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light HSL Server state", __func__);
- return;
- }
- rsp_ctrl = &srv->rsp_ctrl;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: {
- struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light HSL Hue Server state", __func__);
- return;
- }
- rsp_ctrl = &srv->rsp_ctrl;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: {
- struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light HSL Saturation Server state", __func__);
- return;
- }
- rsp_ctrl = &srv->rsp_ctrl;
- break;
- }
- default:
- BT_ERR("%s, Invalid Light HSL Server Model 0x%04x", __func__, model->id);
- return;
- }
- /* Callback the received message to the application layer */
- if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
- return;
- }
- switch (ctx->recv_op) {
- case BLE_MESH_MODEL_OP_LIGHT_HSL_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS;
- break;
- default:
- BT_WARN("%s, Unknown Light HSL Get opcode 0x%04x", __func__, ctx->recv_op);
- return;
- }
- send_light_hsl_status(model, ctx, false, opcode);
- return;
- }
- void light_hsl_publish(struct bt_mesh_model *model, u16_t opcode)
- {
- if (model->user_data == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- switch (model->id) {
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: {
- struct bt_mesh_light_hsl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light HSL Server state", __func__);
- return;
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: {
- struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light HSL Hue Server state", __func__);
- return;
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: {
- struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light HSL Saturation Server state", __func__);
- return;
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV: {
- struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light HSL Setup Server state", __func__);
- return;
- }
- break;
- }
- default:
- BT_ERR("%s, Invalid Light HSL Server Model 0x%04x", __func__, model->id);
- return;
- }
- send_light_hsl_status(model, NULL, true, opcode);
- return;
- }
- static void light_hsl_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_srv *srv = model->user_data;
- u16_t lightness = 0U, hue = 0U, saturation = 0U;
- u8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- s64_t now = 0;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- lightness = net_buf_simple_pull_le16(buf);
- hue = net_buf_simple_pull_le16(buf);
- saturation = net_buf_simple_pull_le16(buf);
- tid = net_buf_simple_pull_u8(buf);
- if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .hsl_set.op_en = optional,
- .hsl_set.lightness = lightness,
- .hsl_set.hue = hue,
- .hsl_set.saturation = saturation,
- .hsl_set.tid = tid,
- .hsl_set.trans_time = trans_time,
- .hsl_set.delay = delay,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
- /* In this condition, no event will be callback to application layer */
- return;
- }
- bt_mesh_light_server_lock();
- bt_mesh_server_stop_transition(&srv->transition);
- bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
- srv->state->target_lightness = lightness;
- if (srv->state->hue_range_min && hue < srv->state->hue_range_min) {
- hue = srv->state->hue_range_min;
- } else if (srv->state->hue_range_max && hue > srv->state->hue_range_max) {
- hue = srv->state->hue_range_max;
- }
- srv->state->target_hue = hue;
- if (srv->state->saturation_range_min && saturation < srv->state->saturation_range_min) {
- saturation = srv->state->saturation_range_min;
- } else if (srv->state->saturation_range_max && saturation > srv->state->saturation_range_max) {
- saturation = srv->state->saturation_range_max;
- }
- srv->state->target_saturation = saturation;
- /**
- * If the target state is equal to the current state, the transition shall not
- * be started and is considered complete.
- */
- if (srv->state->target_lightness != srv->state->lightness ||
- srv->state->target_hue != srv->state->hue ||
- srv->state->target_saturation != srv->state->saturation) {
- light_hsl_tt_values(srv, trans_time, delay);
- } else {
- bt_mesh_light_server_state_change_t change = {
- .hsl_set.lightness = srv->state->lightness,
- .hsl_set.hue = srv->state->hue,
- .hsl_set.saturation = srv->state->saturation,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
- bt_mesh_light_server_unlock();
- return;
- }
- /* Copy the ctx of the received message */
- if (srv->transition.timer.work._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->lightness = srv->state->target_lightness;
- srv->state->hue = srv->state->target_hue;
- srv->state->saturation = srv->state->target_saturation;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_hsl_default_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
- u16_t lightness = 0U, hue = 0U, saturation = 0U;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- lightness = net_buf_simple_pull_le16(buf);
- hue = net_buf_simple_pull_le16(buf);
- saturation = net_buf_simple_pull_le16(buf);
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .hsl_default_set.lightness = lightness,
- .hsl_default_set.hue = hue,
- .hsl_default_set.saturation = saturation,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (srv->state->hue_range_min && hue < srv->state->hue_range_min) {
- hue = srv->state->hue_range_min;
- } else if (srv->state->hue_range_max && hue > srv->state->hue_range_max) {
- hue = srv->state->hue_range_max;
- }
- if (srv->state->saturation_range_min && saturation < srv->state->saturation_range_min) {
- saturation = srv->state->saturation_range_min;
- } else if (srv->state->saturation_range_max && saturation > srv->state->saturation_range_max) {
- saturation = srv->state->saturation_range_max;
- }
- srv->state->lightness_default = lightness;
- srv->state->hue_default = hue;
- srv->state->saturation_default = saturation;
- bt_mesh_light_server_state_change_t change = {
- .hsl_default_set.lightness = srv->state->lightness_default,
- .hsl_default_set.hue = srv->state->hue_default,
- .hsl_default_set.saturation = srv->state->saturation_default,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS);
- return;
- }
- static void light_hsl_range_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
- u16_t hue_min = 0U, hue_max = 0U, saturation_min = 0U, saturation_max = 0U;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- hue_min = net_buf_simple_pull_le16(buf);
- hue_max = net_buf_simple_pull_le16(buf);
- saturation_min = net_buf_simple_pull_le16(buf);
- saturation_max = net_buf_simple_pull_le16(buf);
- if (hue_min > hue_max) {
- BT_ERR("%s, Invalid parameter, Hue min 0x%04x, Hue max 0x%04x",
- __func__, hue_min, hue_max);
- return;
- }
- if (saturation_min > saturation_max) {
- BT_ERR("%s, Invalid parameter, Saturation min 0x%04x, Saturation max 0x%04x",
- __func__, hue_min, hue_max);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .hsl_range_set.hue_range_min = hue_min,
- .hsl_range_set.hue_range_max = hue_max,
- .hsl_range_set.sat_range_min = saturation_min,
- .hsl_range_set.sat_range_max = saturation_max,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
- srv->state->hue_range_min = hue_min;
- srv->state->hue_range_max = hue_max;
- srv->state->saturation_range_min = saturation_min;
- srv->state->saturation_range_max = saturation_max;
- bt_mesh_light_server_state_change_t change = {
- .hsl_range_set.hue_range_min = srv->state->hue_range_min,
- .hsl_range_set.hue_range_max = srv->state->hue_range_max,
- .hsl_range_set.sat_range_min = srv->state->saturation_range_min,
- .hsl_range_set.sat_range_max = srv->state->saturation_range_max,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS);
- return;
- }
- static void light_hsl_hue_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
- u8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- u16_t hue = 0U;
- s64_t now = 0;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- hue = net_buf_simple_pull_le16(buf);
- tid = net_buf_simple_pull_u8(buf);
- if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .hsl_hue_set.op_en = optional,
- .hsl_hue_set.hue = hue,
- .hsl_hue_set.tid = tid,
- .hsl_hue_set.trans_time = trans_time,
- .hsl_hue_set.delay = delay,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
- /* In this condition, no event will be callback to application layer */
- return;
- }
- bt_mesh_light_server_lock();
- bt_mesh_server_stop_transition(&srv->transition);
- bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
- if (srv->state->hue_range_min && hue < srv->state->hue_range_min) {
- hue = srv->state->hue_range_min;
- } else if (srv->state->hue_range_max && hue > srv->state->hue_range_max) {
- hue = srv->state->hue_range_max;
- }
- srv->state->target_hue = hue;
- /**
- * If the target state is equal to the current state, the transition shall not
- * be started and is considered complete.
- */
- if (srv->state->target_hue != srv->state->hue) {
- light_hsl_hue_tt_values(srv, trans_time, delay);
- } else {
- bt_mesh_light_server_state_change_t change = {
- .hsl_hue_set.hue = srv->state->hue,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
- bt_mesh_light_server_unlock();
- return;
- }
- /* Copy the ctx of the received message */
- if (srv->transition.timer.work._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->hue = srv->state->target_hue;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_hsl_sat_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
- u8_t tid = 0U, trans_time = 0U, delay = 0U;
- u16_t saturation = 0U;
- bool optional = false;
- s64_t now = 0;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- saturation = net_buf_simple_pull_le16(buf);
- tid = net_buf_simple_pull_u8(buf);
- if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .hsl_saturation_set.op_en = optional,
- .hsl_saturation_set.saturation = saturation,
- .hsl_saturation_set.tid = tid,
- .hsl_saturation_set.trans_time = trans_time,
- .hsl_saturation_set.delay = delay,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
- /* In this condition, no event will be callback to application layer */
- return;
- }
- bt_mesh_light_server_lock();
- bt_mesh_server_stop_transition(&srv->transition);
- bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
- if (srv->state->saturation_range_min && saturation < srv->state->saturation_range_min) {
- saturation = srv->state->saturation_range_min;
- } else if (srv->state->saturation_range_max && saturation > srv->state->saturation_range_max) {
- saturation = srv->state->saturation_range_max;
- }
- srv->state->target_saturation = saturation;
- /**
- * If the target state is equal to the current state, the transition shall not
- * be started and is considered complete.
- */
- if (srv->state->target_saturation != srv->state->saturation) {
- light_hsl_sat_tt_values(srv, trans_time, delay);
- } else {
- bt_mesh_light_server_state_change_t change = {
- .hsl_saturation_set.saturation = srv->state->saturation,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
- bt_mesh_light_server_unlock();
- return;
- }
- /* Copy the ctx of the received message */
- if (srv->transition.timer.work._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->saturation = srv->state->target_saturation;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- /* Light xyL Server/Setup Server message handlers */
- static void send_light_xyl_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- bool publish, u16_t opcode)
- {
- struct net_buf_simple *msg = NULL;
- u8_t length = 2 + 9;
- if (ctx == NULL && publish == false) {
- BT_ERR("%s, Invalid parameter", __func__);
- return;
- }
- if (publish == false) {
- msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
- if (msg == NULL) {
- BT_ERR("%s, Failed to allocate memory", __func__);
- return;
- }
- } else {
- msg = bt_mesh_server_get_pub_msg(model, length);
- if (msg == NULL) {
- return;
- }
- }
- bt_mesh_model_msg_init(msg, opcode);
- switch (opcode) {
- case BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS:
- case BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS: {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- if (opcode == BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS) {
- net_buf_simple_add_le16(msg, srv->state->lightness);
- net_buf_simple_add_le16(msg, srv->state->x);
- net_buf_simple_add_le16(msg, srv->state->y);
- if (srv->transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->transition);
- net_buf_simple_add_u8(msg, srv->transition.remain_time);
- }
- } else if (opcode == BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS) {
- net_buf_simple_add_le16(msg, srv->state->target_lightness);
- net_buf_simple_add_le16(msg, srv->state->target_x);
- net_buf_simple_add_le16(msg, srv->state->target_y);
- if (srv->transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->transition);
- net_buf_simple_add_u8(msg, srv->transition.remain_time);
- }
- }
- break;
- }
- case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS:
- if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SRV) {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_default);
- net_buf_simple_add_le16(msg, srv->state->x_default);
- net_buf_simple_add_le16(msg, srv->state->y_default);
- } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV) {
- struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
- net_buf_simple_add_le16(msg, srv->state->lightness_default);
- net_buf_simple_add_le16(msg, srv->state->x_default);
- net_buf_simple_add_le16(msg, srv->state->y_default);
- }
- break;
- case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS:
- if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SRV) {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- net_buf_simple_add_u8(msg, srv->state->status_code);
- net_buf_simple_add_le16(msg, srv->state->x_range_min);
- net_buf_simple_add_le16(msg, srv->state->x_range_max);
- net_buf_simple_add_le16(msg, srv->state->y_range_min);
- net_buf_simple_add_le16(msg, srv->state->y_range_max);
- } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV) {
- struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
- net_buf_simple_add_u8(msg, srv->state->status_code);
- net_buf_simple_add_le16(msg, srv->state->x_range_min);
- net_buf_simple_add_le16(msg, srv->state->x_range_max);
- net_buf_simple_add_le16(msg, srv->state->y_range_min);
- net_buf_simple_add_le16(msg, srv->state->y_range_max);
- }
- break;
- default:
- BT_WARN("%s, Unknown Light xyL status opcode 0x%04x", __func__, opcode);
- if (publish == false) {
- bt_mesh_free_buf(msg);
- }
- return;
- }
- if (publish == false) {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
- bt_mesh_free_buf(msg);
- } else {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
- }
- return;
- }
- static void light_xyl_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- u16_t opcode = 0U;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
- return;
- }
- switch (ctx->recv_op) {
- case BLE_MESH_MODEL_OP_LIGHT_XYL_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS;
- break;
- default:
- BT_WARN("%s, Unknown Light xyL Get opcode 0x%04x", __func__, ctx->recv_op);
- return;
- }
- send_light_xyl_status(model, ctx, false, opcode);
- return;
- }
- void light_xyl_publish(struct bt_mesh_model *model, u16_t opcode)
- {
- if (model->user_data == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- switch (model->id) {
- case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light xyL Server state", __func__);
- return;
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV: {
- struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, Invalid Light xyL Setup Server state", __func__);
- return;
- }
- break;
- }
- default:
- BT_ERR("%s, Invalid Light xyL Server Model 0x%04x", __func__, model->id);
- return;
- }
- send_light_xyl_status(model, NULL, true, opcode);
- return;
- }
- static void light_xyl_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- u8_t tid = 0U, trans_time = 0U, delay = 0U;
- u16_t lightness = 0U, x = 0U, y = 0U;
- bool optional = false;
- s64_t now = 0;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- lightness = net_buf_simple_pull_le16(buf);
- x = net_buf_simple_pull_le16(buf);
- y = net_buf_simple_pull_le16(buf);
- tid = net_buf_simple_pull_u8(buf);
- if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .xyl_set.op_en = optional,
- .xyl_set.lightness = lightness,
- .xyl_set.x = x,
- .xyl_set.y = y,
- .xyl_set.tid = tid,
- .xyl_set.trans_time = trans_time,
- .xyl_set.delay = delay,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) {
- send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
- }
- send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
- /* In this condition, no event will be callback to application layer */
- return;
- }
- bt_mesh_light_server_lock();
- bt_mesh_server_stop_transition(&srv->transition);
- bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
- srv->state->target_lightness = lightness;
- if (srv->state->x_range_min && x < srv->state->x_range_min) {
- x = srv->state->x_range_min;
- } else if (srv->state->x_range_max && x > srv->state->x_range_max) {
- x = srv->state->x_range_max;
- }
- srv->state->target_x = x;
- if (srv->state->y_range_min && y < srv->state->y_range_min) {
- y = srv->state->y_range_min;
- } else if (srv->state->y_range_max && y > srv->state->y_range_max) {
- y = srv->state->y_range_max;
- }
- srv->state->target_y = y;
- /**
- * If the target state is equal to the current state, the transition shall not
- * be started and is considered complete.
- */
- if (srv->state->target_lightness != srv->state->lightness ||
- srv->state->target_x != srv->state->x ||
- srv->state->target_y != srv->state->y) {
- light_xyl_tt_values(srv, trans_time, delay);
- } else {
- bt_mesh_light_server_state_change_t change = {
- .xyl_set.lightness = srv->state->lightness,
- .xyl_set.x = srv->state->x,
- .xyl_set.y = srv->state->y,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) {
- send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
- }
- send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
- bt_mesh_light_server_unlock();
- return;
- }
- /* Copy the ctx of the received message */
- if (srv->transition.timer.work._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->lightness = srv->state->target_lightness;
- srv->state->x = srv->state->target_x;
- srv->state->y = srv->state->target_y;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) {
- send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
- }
- send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_xyl_default_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
- u16_t lightness = 0U, x = 0U, y = 0U;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- lightness = net_buf_simple_pull_le16(buf);
- x = net_buf_simple_pull_le16(buf);
- y = net_buf_simple_pull_le16(buf);
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .xyl_default_set.lightness = lightness,
- .xyl_default_set.x = x,
- .xyl_default_set.y = y,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (srv->state->x_range_min && x < srv->state->x_range_min) {
- x = srv->state->x_range_min;
- } else if (srv->state->x_range_max && x > srv->state->x_range_max) {
- x = srv->state->x_range_max;
- }
- if (srv->state->y_range_min && y < srv->state->y_range_min) {
- y = srv->state->y_range_min;
- } else if (srv->state->y_range_max && y > srv->state->y_range_max) {
- y = srv->state->y_range_max;
- }
- srv->state->lightness_default = lightness;
- srv->state->x_default = x;
- srv->state->y_default = y;
- bt_mesh_light_server_state_change_t change = {
- .xyl_default_set.lightness = srv->state->lightness_default,
- .xyl_default_set.x = srv->state->x_default,
- .xyl_default_set.y = srv->state->y_default,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET) {
- send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS);
- }
- send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS);
- return;
- }
- static void light_xyl_range_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
- u16_t x_min = 0U, x_max = 0U, y_min = 0U, y_max = 0U;
- if (srv == NULL || srv->state == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- x_min = net_buf_simple_pull_le16(buf);
- x_max = net_buf_simple_pull_le16(buf);
- y_min = net_buf_simple_pull_le16(buf);
- y_max = net_buf_simple_pull_le16(buf);
- if (x_min > x_max) {
- BT_ERR("%s, Invalid parameter, xyL x min 0x%04x, xyL x max 0x%04x",
- __func__, x_min, x_max);
- return;
- }
- if (y_min > y_max) {
- BT_ERR("%s, Invalid parameter, xyL y min 0x%04x, xyL y max 0x%04x",
- __func__, y_min, y_max);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .xyl_range_set.x_range_min = x_min,
- .xyl_range_set.x_range_max = x_max,
- .xyl_range_set.y_range_min = y_min,
- .xyl_range_set.y_range_max = y_max,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
- srv->state->x_range_min = x_min;
- srv->state->x_range_max = x_max;
- srv->state->y_range_min = y_min;
- srv->state->y_range_max = y_max;
- bt_mesh_light_server_state_change_t change = {
- .xyl_range_set.x_range_min = srv->state->x_range_min,
- .xyl_range_set.x_range_max = srv->state->x_range_max,
- .xyl_range_set.y_range_min = srv->state->y_range_min,
- .xyl_range_set.y_range_max = srv->state->y_range_max,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET) {
- send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS);
- }
- send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS);
- return;
- }
- /* Light LC Server/Setup Server message handlers */
- static void send_light_lc_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- bool publish, u16_t opcode)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- struct net_buf_simple *msg = NULL;
- u8_t length = 2 + 3;
- if (ctx == NULL && publish == false) {
- BT_ERR("%s, Invalid parameter", __func__);
- return;
- }
- if (publish == false) {
- msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
- if (msg == NULL) {
- BT_ERR("%s, Failed to allocate memory", __func__);
- return;
- }
- } else {
- msg = bt_mesh_server_get_pub_msg(model, length);
- if (msg == NULL) {
- return;
- }
- }
- bt_mesh_model_msg_init(msg, opcode);
- switch (opcode) {
- case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS:
- net_buf_simple_add_u8(msg, srv->lc->state.mode);
- break;
- case BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS:
- net_buf_simple_add_u8(msg, srv->lc->state.occupancy_mode);
- break;
- case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS:
- net_buf_simple_add_u8(msg, srv->lc->state.light_onoff);
- if (srv->transition.counter) {
- bt_mesh_server_calc_remain_time(&srv->transition);
- net_buf_simple_add_u8(msg, srv->lc->state.target_light_onoff);
- net_buf_simple_add_u8(msg, srv->transition.remain_time);
- }
- break;
- default:
- BT_WARN("%s, Unknown Light LC status opcode 0x%04x", __func__, opcode);
- if (publish == false) {
- bt_mesh_free_buf(msg);
- }
- return;
- }
- if (publish == false) {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
- bt_mesh_free_buf(msg);
- } else {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
- }
- return;
- }
- static void light_lc_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- u16_t opcode = 0U;
- if (srv == NULL || srv->lc == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
- return;
- }
- switch (ctx->recv_op) {
- case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS;
- break;
- case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET:
- opcode = BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS;
- break;
- default:
- BT_WARN("%s, Unknown Light LC Get opcode 0x%04x", __func__, ctx->recv_op);
- return;
- }
- send_light_lc_status(model, ctx, false, opcode);
- return;
- }
- void light_lc_publish(struct bt_mesh_model *model, u16_t opcode)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- if (srv == NULL || srv->lc == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- send_light_lc_status(model, NULL, true, opcode);
- return;
- }
- static void light_lc_mode_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- u8_t mode = 0U;
- if (srv == NULL || srv->lc == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- mode = net_buf_simple_pull_u8(buf);
- if (mode > BLE_MESH_STATE_ON) {
- BT_ERR("%s, Invalid LC Mode 0x%02x", __func__, mode);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .lc_mode_set.mode = mode,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- srv->lc->state.mode = mode;
- bt_mesh_light_server_state_change_t change = {
- .lc_mode_set.mode = srv->lc->state.mode,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET) {
- send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS);
- }
- send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS);
- return;
- }
- static void light_lc_om_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- u8_t om = 0U;
- if (srv == NULL || srv->lc == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- om = net_buf_simple_pull_u8(buf);
- if (om > BLE_MESH_STATE_ON) {
- BT_ERR("%s, Invalid LC Occupancy Mode 0x%02x", __func__, om);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .lc_om_set.mode = om,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- srv->lc->state.occupancy_mode = om;
- bt_mesh_light_server_state_change_t change = {
- .lc_om_set.mode = srv->lc->state.occupancy_mode,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET) {
- send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS);
- }
- send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS);
- return;
- }
- static void light_lc_light_onoff_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- u8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- u8_t onoff = 0U;
- s64_t now = 0;
- if (srv == NULL || srv->lc == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- onoff = net_buf_simple_pull_u8(buf);
- tid = net_buf_simple_pull_u8(buf);
- if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .lc_light_onoff_set.op_en = optional,
- .lc_light_onoff_set.light_onoff = onoff,
- .lc_light_onoff_set.tid = tid,
- .lc_light_onoff_set.trans_time = trans_time,
- .lc_light_onoff_set.delay = delay,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) {
- send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
- }
- send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
- /* In this condition, no event will be callback to application layer */
- return;
- }
- bt_mesh_light_server_lock();
- bt_mesh_server_stop_transition(&srv->transition);
- bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
- srv->lc->state.target_light_onoff = onoff;
- if (srv->lc->state.target_light_onoff != srv->lc->state.light_onoff) {
- light_lc_tt_values(srv, trans_time, delay);
- } else {
- bt_mesh_light_server_state_change_t change = {
- .lc_light_onoff_set.onoff = srv->lc->state.light_onoff,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) {
- send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
- }
- send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
- bt_mesh_light_server_unlock();
- return;
- }
- /* Copy the ctx of the received message */
- if (srv->transition.timer.work._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->lc->state.light_onoff = srv->lc->state.target_light_onoff;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) {
- send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
- }
- send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_lc_sensor_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- /**
- * When a Light LC Server receives a Sensor Status message, and if the message
- * Raw field contains a Raw Value for the Motion Sensed Property, and the value
- * is greater than 0, or a Raw Value for the People Count Property, and the
- * value is greater than 0, or a Raw Value for the Presence Detected Property,
- * and the value is greater than 0, then it shall set the Light LC Occupancy
- * state to 0b1.
- * If the message Raw field contains a Raw Value for the Time Since Motion Sensed
- * device property, which represents a value less than or equal to the value of
- * the Light LC Occupancy Delay state, it shall delay setting the Light LC Occupancy
- * state to 0b1 by the difference between the value of the Light LC Occupancy Delay
- * state and the received Time Since Motion value.
- * When a Light LC Server receives a Sensor Status message, and if the message Raw
- * field contains a Raw Value for the Present Ambient Light Level device property,
- * it shall set the Light LC Ambient LuxLevel state to the Represented Value of the
- * received Present Ambient Light Level.
- *
- * Motion Sensed: 1 octet, 0x0042
- * People Count: 2 octets, 0x004C
- * Presence Detected: 1 octet, 0x004D
- *
- * Time Since Motion Sensed: 2 octets, 0x0068
- *
- * Present Ambient Light Level: 4 octets, 0x004E
- */
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- bt_mesh_light_server_state_change_t change = {0};
- u16_t mpid = 0U, prop_id = 0U;
- u8_t length = 0U;
- if (srv == NULL || srv->lc == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- if (srv->rsp_ctrl.status_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_status_msg_t status = {
- .sensor_status.data = buf,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_STATUS_MSG, model, ctx, (const u8_t *)&status, sizeof(status));
- return;
- }
- mpid = net_buf_simple_pull_le16(buf);
- if (mpid & BIT(0)) {
- length = (u8_t)((mpid & 0xff) >> 1);
- u8_t msb = net_buf_simple_pull_u8(buf);
- prop_id = (u16_t)(msb << 8) | (u16_t)(mpid >> 8);
- } else {
- length = (u8_t)((mpid & 0x1f) >> 1);
- prop_id = (u16_t)(mpid >> 5);
- }
- change.sensor_status.property_id = prop_id;
- switch (prop_id) {
- case BLE_MESH_MOTION_SENSED: {
- if (length != BLE_MESH_MOTION_SENSED_LEN || length != buf->len) {
- BT_WARN("%s, Invalid Motion Sensed Property length", __func__);
- return;
- }
- u8_t val = net_buf_simple_pull_u8(buf);
- if (val > 0) {
- srv->lc->state.occupancy = BLE_MESH_STATE_ON;
- change.sensor_status.state.occupancy = srv->lc->state.occupancy;
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- }
- break;
- }
- case BLE_MESH_PEOPLE_COUNT: {
- if (length != BLE_MESH_PEOPLE_COUNT_LEN || length != buf->len) {
- BT_WARN("%s, Invalid Motion Sensed Property length", __func__);
- return;
- }
- u16_t val = net_buf_simple_pull_le16(buf);
- if (val > 0) {
- srv->lc->state.occupancy = BLE_MESH_STATE_ON;
- change.sensor_status.state.occupancy = srv->lc->state.occupancy;
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- }
- break;
- }
- case BLE_MESH_PRESENCE_DETECTED: {
- if (length != BLE_MESH_PRESENCE_DETECTED_LEN || length != buf->len) {
- BT_WARN("%s, Invalid Motion Sensed Property length", __func__);
- return;
- }
- u8_t val = net_buf_simple_pull_u8(buf);
- if (val > 0) {
- srv->lc->state.occupancy = BLE_MESH_STATE_ON;
- change.sensor_status.state.occupancy = srv->lc->state.occupancy;
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- }
- break;
- }
- case BLE_MESH_TIME_SINCE_MOTION_SENSED: {
- if (length != BLE_MESH_TIME_SINCE_MOTION_SENSED_LEN || length != buf->len) {
- BT_WARN("%s, Invalid Motion Sensed Property length", __func__);
- return;
- }
- u16_t val = net_buf_simple_pull_le16(buf);
- if (val <= srv->lc->prop_state.time_occupancy_delay) {
- srv->lc->prop_state.set_occupancy_to_1_delay =
- srv->lc->prop_state.time_occupancy_delay - val;
- change.sensor_status.state.set_occupancy_to_1_delay = srv->lc->prop_state.set_occupancy_to_1_delay;
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- }
- break;
- }
- case BLE_MESH_PRESENT_AMBIENT_LIGHT_LEVEL: {
- /**
- * Present Ambient Light Level device property is 4 octets, but ambient
- * luxlevel length is 3 octets, and other devices may send Sensor Status
- * which only contains 3 octets just for Light LC Server.
- * Here we just check if the length is larger than 3.
- */
- if (buf->len < 3) {
- BT_WARN("%s, Invalid Motion Sensed Property length", __func__);
- return;
- }
- u16_t lsb = net_buf_simple_pull_le16(buf);
- u8_t msb = net_buf_simple_pull_u8(buf);
- srv->lc->state.ambient_luxlevel = (msb << 16) | lsb;
- change.sensor_status.state.ambient_luxlevel = srv->lc->state.ambient_luxlevel;
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- break;
- }
- default:
- break;
- }
- }
- static u8_t *get_light_lc_prop_val(struct bt_mesh_model *model, u16_t prop_id)
- {
- struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
- u8_t *val = NULL;
- switch (prop_id) {
- case BLE_MESH_LIGHT_CONTROL_TIME_OCCUPANCY_DELAY:
- val = (u8_t *)&srv->lc->prop_state.time_occupancy_delay;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_FADE_ON:
- val = (u8_t *)&srv->lc->prop_state.time_fade_on;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_RUN_ON:
- val = (u8_t *)&srv->lc->prop_state.time_run_on;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_FADE:
- val = (u8_t *)&srv->lc->prop_state.time_fade;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_PROLONG:
- val = (u8_t *)&srv->lc->prop_state.time_prolong;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_AUTO:
- val = (u8_t *)&srv->lc->prop_state.time_fade_standby_auto;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_MANUAL:
- val = (u8_t *)&srv->lc->prop_state.time_fade_standby_manual;
- break;
- case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_ON:
- val = (u8_t *)&srv->lc->prop_state.lightness_on;
- break;
- case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_PROLONG:
- val = (u8_t *)&srv->lc->prop_state.lightness_prolong;
- break;
- case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_STANDBY:
- val = (u8_t *)&srv->lc->prop_state.lightness_standby;
- break;
- case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_ON:
- val = (u8_t *)&srv->lc->prop_state.ambient_luxlevel_on;
- break;
- case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_PROLONG:
- val = (u8_t *)&srv->lc->prop_state.ambient_luxlevel_prolong;
- break;
- case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_STANDBY:
- val = (u8_t *)&srv->lc->prop_state.ambient_luxlevel_standby;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_KIU:
- val = (u8_t *)&srv->lc->prop_state.regulator_kiu;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_KID:
- val = (u8_t *)&srv->lc->prop_state.regulator_kid;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_KPU:
- val = (u8_t *)&srv->lc->prop_state.regulator_kpu;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_KPD:
- val = (u8_t *)&srv->lc->prop_state.regulator_kpd;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_ACCURACY:
- val = (u8_t *)&srv->lc->prop_state.regulator_accuracy;
- break;
- }
- return val;
- }
- u8_t *bt_mesh_get_lc_prop_value(struct bt_mesh_model *model, u16_t prop_id)
- {
- if (model == NULL) {
- BT_ERR("%s, Invalid parameter", __func__);
- return NULL;
- }
- return get_light_lc_prop_val(model, prop_id);
- }
- static void send_light_lc_prop_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- u16_t prop_id, bool publish)
- {
- struct net_buf_simple *msg = NULL;
- u8_t length = 1 + 2 + 4;
- u8_t *prop_val = NULL;
- prop_val = get_light_lc_prop_val(model, prop_id);
- if (prop_val == NULL) {
- BT_ERR("%s, Failed to get Light LC Property value", __func__);
- return;
- }
- if (publish == false) {
- msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
- if (msg == NULL) {
- BT_ERR("%s, Failed to allocate memory", __func__);
- return;
- }
- } else {
- msg = bt_mesh_server_get_pub_msg(model, length);
- if (msg == NULL) {
- return;
- }
- }
- bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS);
- net_buf_simple_add_le16(msg, prop_id);
- net_buf_simple_add_mem(msg, prop_val, bt_mesh_get_dev_prop_len(prop_id));
- if (publish == false) {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
- bt_mesh_free_buf(msg);
- } else {
- BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
- }
- return;
- }
- static void light_lc_prop_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
- u16_t prop_id = 0U;
- if (srv == NULL || srv->lc == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- prop_id = net_buf_simple_pull_le16(buf);
- if (prop_id < 0x002B || prop_id > 0x003C) {
- BT_ERR("%s, Invalid Light LC Property ID 0x%04x", __func__, prop_id);
- return;
- }
- /* Callback the received message to the application layer */
- if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_get_msg_t get = {
- .lc_property_get.id = net_buf_simple_pull_le16(buf),
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get));
- return;
- }
- send_light_lc_prop_status(model, ctx, prop_id, false);
- return;
- }
- static void light_lc_prop_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
- u8_t *prop_val = NULL, expect_len = 0U;
- u16_t prop_id = 0U;
- if (srv == NULL || srv->lc == NULL) {
- BT_ERR("%s, Invalid model user_data", __func__);
- return;
- }
- prop_id = net_buf_simple_pull_le16(buf);
- if (prop_id < 0x002B || prop_id > 0x003C) {
- BT_ERR("%s, Invalid Light LC Property ID 0x%04x", __func__, prop_id);
- return;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
- bt_mesh_light_server_recv_set_msg_t set = {
- .lc_property_set.id = net_buf_simple_pull_le16(buf),
- .lc_property_set.value = buf,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set));
- return;
- }
- expect_len = bt_mesh_get_dev_prop_len(prop_id);
- if (buf->len != expect_len) {
- BT_ERR("%s, Invalid Light LC Property length, ID 0x%04x, expect %d, actual %d",
- __func__, prop_id, expect_len, buf->len);
- return;
- }
- prop_val = get_light_lc_prop_val(model, prop_id);
- if (prop_val == NULL) {
- BT_ERR("%s, Failed to get Light LC Property value", __func__);
- return;
- }
- memcpy(prop_val, buf->data, buf->len);
- bt_mesh_light_server_state_change_t change = {
- .lc_property_set.id = prop_id,
- .lc_property_set.value = buf,
- };
- bt_mesh_lighting_server_cb_evt_to_btc(
- BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change));
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET) {
- send_light_lc_prop_status(model, ctx, prop_id, false);
- }
- send_light_lc_prop_status(model, ctx, prop_id, true);
- return;
- }
- /* message handlers (End) */
- /* Mapping of message handlers for Light Lightness Server (0x1300) */
- const struct bt_mesh_model_op light_lightness_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET, 0, light_lightness_get },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET, 3, light_lightness_set },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK, 3, light_lightness_set },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET, 0, light_lightness_get },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET, 3, light_lightness_linear_set },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK, 3, light_lightness_linear_set },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET, 0, light_lightness_get },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET, 0, light_lightness_get },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET, 0, light_lightness_get },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light Lightness Setup Server (0x1301) */
- const struct bt_mesh_model_op light_lightness_setup_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET, 2, light_lightness_default_set },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK, 2, light_lightness_default_set },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET, 4, light_lightness_range_set },
- { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK, 4, light_lightness_range_set },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light CTL Server (0x1303) */
- const struct bt_mesh_model_op light_ctl_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_CTL_GET, 0, light_ctl_get },
- { BLE_MESH_MODEL_OP_LIGHT_CTL_SET, 7, light_ctl_set },
- { BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK, 7, light_ctl_set },
- { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET, 0, light_ctl_get },
- { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET, 0, light_ctl_get },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light CTL Setup Server (0x1304) */
- const struct bt_mesh_model_op light_ctl_setup_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET, 6, light_ctl_default_set },
- { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK, 6, light_ctl_default_set },
- { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET, 4, light_ctl_temp_range_set },
- { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK, 4, light_ctl_temp_range_set },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light CTL Temperature Server (0x1306) */
- const struct bt_mesh_model_op light_ctl_temp_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET, 0, light_ctl_get },
- { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET, 5, light_ctl_temp_set },
- { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK, 5, light_ctl_temp_set },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light HSL Server (0x1307) */
- const struct bt_mesh_model_op light_hsl_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_HSL_GET, 0, light_hsl_get },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_SET, 7, light_hsl_set },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK, 7, light_hsl_set },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET, 0, light_hsl_get },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET, 0, light_hsl_get },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET, 0, light_hsl_get },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light HSL Setup Server (0x1308) */
- const struct bt_mesh_model_op light_hsl_setup_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET, 6, light_hsl_default_set },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK, 6, light_hsl_default_set },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET, 8, light_hsl_range_set },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK, 8, light_hsl_range_set },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light HSL Hue Server (0x130A) */
- const struct bt_mesh_model_op light_hsl_hue_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET, 0, light_hsl_get },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET, 3, light_hsl_hue_set },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK, 3, light_hsl_hue_set },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light HSL Saturation Server (0x130B) */
- const struct bt_mesh_model_op light_hsl_sat_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET, 0, light_hsl_get },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET, 3, light_hsl_sat_set },
- { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK, 3, light_hsl_sat_set },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light xyL Server (0x130C) */
- const struct bt_mesh_model_op light_xyl_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_XYL_GET, 0, light_xyl_get },
- { BLE_MESH_MODEL_OP_LIGHT_XYL_SET, 7, light_xyl_set },
- { BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK, 7, light_xyl_set },
- { BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET, 0, light_xyl_get },
- { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET, 0, light_xyl_get },
- { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET, 0, light_xyl_get },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light xyL Setup Server (0x130D) */
- const struct bt_mesh_model_op light_xyl_setup_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET, 6, light_xyl_default_set },
- { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK, 6, light_xyl_default_set },
- { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET, 8, light_xyl_range_set },
- { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK, 8, light_xyl_range_set },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light LC Server (0x130F) */
- const struct bt_mesh_model_op light_lc_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET, 0, light_lc_get },
- { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET, 1, light_lc_mode_set },
- { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK, 1, light_lc_mode_set },
- { BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET, 0, light_lc_get },
- { BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET, 1, light_lc_om_set },
- { BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK, 1, light_lc_om_set },
- { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET, 0, light_lc_get },
- { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET, 2, light_lc_light_onoff_set },
- { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK, 2, light_lc_light_onoff_set },
- { BLE_MESH_MODEL_OP_SENSOR_STATUS, 3, light_lc_sensor_status },
- BLE_MESH_MODEL_OP_END,
- };
- /* Mapping of message handlers for Light LC Setup Server (0x1310) */
- const struct bt_mesh_model_op light_lc_setup_srv_op[] = {
- { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET, 2, light_lc_prop_get },
- { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET, 3, light_lc_prop_set },
- { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK, 3, light_lc_prop_set },
- BLE_MESH_MODEL_OP_END,
- };
- static int light_server_init(struct bt_mesh_model *model)
- {
- if (model->user_data == NULL) {
- BT_ERR("%s, No Light Server context provided, model_id 0x%04x", __func__, model->id);
- return -EINVAL;
- }
- switch (model->id) {
- case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light Lightness State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_alloc_ctx(&srv->actual_transition.timer.work);
- bt_mesh_server_alloc_ctx(&srv->linear_transition.timer.work);
- k_delayed_work_init(&srv->actual_transition.timer, light_lightness_actual_work_handler);
- k_delayed_work_init(&srv->linear_transition.timer, light_lightness_linear_work_handler);
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV: {
- struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light Lightness State", __func__);
- return -EINVAL;
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
- struct bt_mesh_light_ctl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light CTL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
- k_delayed_work_init(&srv->transition.timer, light_ctl_work_handler);
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV: {
- struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light CTL State", __func__);
- return -EINVAL;
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
- struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light CTL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
- k_delayed_work_init(&srv->transition.timer, light_ctl_temp_work_handler);
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: {
- struct bt_mesh_light_hsl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light HSL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
- k_delayed_work_init(&srv->transition.timer, light_hsl_work_handler);
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV: {
- struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light HSL State", __func__);
- return -EINVAL;
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: {
- struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light HSL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
- k_delayed_work_init(&srv->transition.timer, light_hsl_hue_work_handler);
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: {
- struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light HSL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
- k_delayed_work_init(&srv->transition.timer, light_hsl_sat_work_handler);
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light xyL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
- k_delayed_work_init(&srv->transition.timer, light_xyl_work_handler);
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV: {
- struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light xyL State", __func__);
- return -EINVAL;
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_LC_SRV: {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- if (srv->lc == NULL) {
- BT_ERR("%s, NULL Light LC State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
- k_delayed_work_init(&srv->transition.timer, light_lc_work_handler);
- }
- srv->model = model;
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV: {
- struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
- if (srv->lc == NULL) {
- BT_ERR("%s, NULL Light LC State", __func__);
- return -EINVAL;
- }
- srv->model = model;
- break;
- }
- default:
- BT_WARN("%s, Unknown Light Server Model, model_id 0x%04x", __func__, model->id);
- return -EINVAL;
- }
- bt_mesh_light_server_mutex_new();
- return 0;
- }
- int bt_mesh_light_lightness_srv_init(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light Lightness Server has no publication support", __func__);
- return -EINVAL;
- }
- /* When this model is present on an Element, the corresponding Light Lightness
- * Setup Server model shall also be present.
- */
- struct bt_mesh_elem *element = bt_mesh_model_elem(model);
- if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) == NULL) {
- BT_WARN("%s, Light Lightness Setup Server is not present", __func__);
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- int bt_mesh_light_lightness_setup_srv_init(struct bt_mesh_model *model, bool primary)
- {
- return light_server_init(model);
- }
- int bt_mesh_light_ctl_srv_init(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light CTL Server has no publication support", __func__);
- return -EINVAL;
- }
- /**
- * When this model is present on an Element, the corresponding Light CTL
- * Temperature Server model and the corresponding Light CTL Setup Server
- * model shall also be present.
- * The model requires two elements: the main element and the Temperature
- * element. The Temperature element contains the corresponding Light CTL
- * Temperature Server model.
- */
- struct bt_mesh_elem *element = bt_mesh_model_elem(model);
- if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) == NULL) {
- BT_WARN("%s, Light CTL Setup Server is not present", __func__);
- /* Just give a warning here, continue with the initialization */
- }
- if (bt_mesh_elem_count() < 2) {
- BT_WARN("%s, Light CTL Server requires two elements", __func__);
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- int bt_mesh_light_ctl_setup_srv_init(struct bt_mesh_model *model, bool primary)
- {
- return light_server_init(model);
- }
- int bt_mesh_light_ctl_temp_srv_init(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light CTL Temperature Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_init(model);
- }
- int bt_mesh_light_hsl_srv_init(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light HSL Server has no publication support", __func__);
- return -EINVAL;
- }
- /**
- * When this model is present on an Element, the corresponding Light HSL Hue
- * Server model and the corresponding Light HSL Saturation Server model and
- * the corresponding Light HSL Setup Server model shall also be present.
- * The model requires three elements: the main element and the Hue element
- * and the Saturation element. The Hue element contains the corresponding
- * Light HSL Hue Server model, and the Saturation element contains the corr-
- * esponding Light HSL Saturation Server model.
- */
- struct bt_mesh_elem *element = bt_mesh_model_elem(model);
- if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) == NULL) {
- BT_WARN("%s, Light HSL Setup Server is not present", __func__);
- /* Just give a warning here, continue with the initialization */
- }
- if (bt_mesh_elem_count() < 3) {
- BT_WARN("%s, Light HSL Server requires three elements", __func__);
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- int bt_mesh_light_hsl_setup_srv_init(struct bt_mesh_model *model, bool primary)
- {
- return light_server_init(model);
- }
- int bt_mesh_light_hsl_hue_srv_init(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light HSL Hue Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_init(model);
- }
- int bt_mesh_light_hsl_sat_srv_init(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light HSL Saturation Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_init(model);
- }
- int bt_mesh_light_xyl_srv_init(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light xyL Server has no publication support", __func__);
- return -EINVAL;
- }
- /**
- * When this model is present on an Element, the corresponding Light xyL
- * Setup Server model shall also be present.
- */
- struct bt_mesh_elem *element = bt_mesh_model_elem(model);
- if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV) == NULL) {
- BT_WARN("%s, Light xyL Setup Server is not present", __func__);
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- int bt_mesh_light_xyl_setup_srv_init(struct bt_mesh_model *model, bool primary)
- {
- return light_server_init(model);
- }
- int bt_mesh_light_lc_srv_init(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light LC Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_init(model);
- }
- int bt_mesh_light_lc_setup_srv_init(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light LC Setup Server has no publication support", __func__);
- return -EINVAL;
- }
- /**
- * When this model is present on an Element, the corresponding Light LC
- * Setup Server model shall also be present.
- */
- struct bt_mesh_elem *element = bt_mesh_model_elem(model);
- if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV) == NULL) {
- BT_WARN("%s, Light LC Setup Server is not present", __func__);
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- static int light_server_deinit(struct bt_mesh_model *model)
- {
- if (model->user_data == NULL) {
- BT_ERR("%s, No Light Server context provided, model_id 0x%04x", __func__, model->id);
- return -EINVAL;
- }
- switch (model->id) {
- case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light Lightness State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_free_ctx(&srv->actual_transition.timer.work);
- bt_mesh_server_free_ctx(&srv->linear_transition.timer.work);
- k_delayed_work_free(&srv->actual_transition.timer);
- k_delayed_work_free(&srv->linear_transition.timer);
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
- struct bt_mesh_light_ctl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light CTL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_free_ctx(&srv->transition.timer.work);
- k_delayed_work_free(&srv->transition.timer);
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
- struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light CTL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_free_ctx(&srv->transition.timer.work);
- k_delayed_work_free(&srv->transition.timer);
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: {
- struct bt_mesh_light_hsl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light HSL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_free_ctx(&srv->transition.timer.work);
- k_delayed_work_free(&srv->transition.timer);
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: {
- struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light HSL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_free_ctx(&srv->transition.timer.work);
- k_delayed_work_free(&srv->transition.timer);
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: {
- struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light HSL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_free_ctx(&srv->transition.timer.work);
- k_delayed_work_free(&srv->transition.timer);
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- if (srv->state == NULL) {
- BT_ERR("%s, NULL Light xyL State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_free_ctx(&srv->transition.timer.work);
- k_delayed_work_free(&srv->transition.timer);
- }
- break;
- }
- case BLE_MESH_MODEL_ID_LIGHT_LC_SRV: {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- if (srv->lc == NULL) {
- BT_ERR("%s, NULL Light LC State", __func__);
- return -EINVAL;
- }
- if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
- bt_mesh_server_free_ctx(&srv->transition.timer.work);
- k_delayed_work_free(&srv->transition.timer);
- }
- break;
- }
- default:
- BT_WARN("%s, Unknown Light Server Model, model_id 0x%04x", __func__, model->id);
- return -EINVAL;
- }
- bt_mesh_light_server_mutex_free();
- return 0;
- }
- int bt_mesh_light_lightness_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light Lightness Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- int bt_mesh_light_lightness_setup_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- return light_server_deinit(model);
- }
- int bt_mesh_light_ctl_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light CTL Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- int bt_mesh_light_ctl_setup_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- return light_server_deinit(model);
- }
- int bt_mesh_light_ctl_temp_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light CTL Temperature Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- int bt_mesh_light_hsl_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light HSL Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- int bt_mesh_light_hsl_setup_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- return light_server_deinit(model);
- }
- int bt_mesh_light_hsl_hue_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light HSL Hue Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- int bt_mesh_light_hsl_sat_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light HSL Saturation Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- int bt_mesh_light_xyl_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light xyL Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- int bt_mesh_light_xyl_setup_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- return light_server_deinit(model);
- }
- int bt_mesh_light_lc_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light LC Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- int bt_mesh_light_lc_setup_srv_deinit(struct bt_mesh_model *model, bool primary)
- {
- if (model->pub == NULL) {
- BT_ERR("%s, Light LC Setup Server has no publication support", __func__);
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
|