| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597 |
- /* Bluetooth: Mesh Lighting Server Models
- *
- * SPDX-FileCopyrightText: 2018 Vikrant More
- * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #include <errno.h>
- #include "btc_ble_mesh_lighting_model.h"
- #include "mesh/config.h"
- #include "access.h"
- #include "transport.h"
- #include "mesh/model_opcode.h"
- #include "mesh/state_transition.h"
- #include "mesh/device_property.h"
- #if CONFIG_BLE_MESH_LIGHTING_SERVER
- static bt_mesh_mutex_t 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, uint16_t opcode)
- {
- struct net_buf_simple *msg = NULL;
- uint8_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, Out of 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("Unknown Light Lightness status opcode 0x%04x", 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));
- }
- }
- 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;
- uint16_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("Unknown Light Lightness Get opcode 0x%04x", ctx->recv_op);
- return;
- }
- send_light_lightness_status(model, ctx, false, opcode);
- }
- void light_lightness_publish(struct bt_mesh_model *model, uint16_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("Invalid Light Lightness Server state");
- 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("Invalid Light Lightness Setup Server state");
- return;
- }
- break;
- }
- default:
- BT_ERR("Invalid Light Lightness Server model 0x%04x", model->id);
- return;
- }
- send_light_lightness_status(model, NULL, true, opcode);
- }
- 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;
- uint8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- uint16_t actual = 0U;
- int64_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 uint8_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 uint8_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.user_data) {
- memcpy(srv->actual_transition.timer.work.user_data, 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);
- }
- 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;
- uint8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- uint16_t linear = 0U;
- int64_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 uint8_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 uint8_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.user_data) {
- memcpy(srv->linear_transition.timer.work.user_data, 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);
- }
- 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;
- uint16_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 uint8_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 uint8_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);
- }
- 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;
- uint16_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("Range min 0x%04x is greater than range max 0x%04x",
- 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 uint8_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 uint8_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);
- }
- /* 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, uint16_t opcode)
- {
- struct net_buf_simple *msg = NULL;
- uint8_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, Out of 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("Unknown Light CTL status opcode 0x%04x", 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));
- }
- }
- 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;
- uint16_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("Invalid Light CTL Server state");
- 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("Invalid Light CTL Temperature Server state");
- return;
- }
- rsp_ctrl = &srv->rsp_ctrl;
- break;
- }
- default:
- BT_ERR("Invalid Light CTL Server model 0x%04x", 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("Unknown Light CTL Get opcode 0x%04x", ctx->recv_op);
- return;
- }
- send_light_ctl_status(model, ctx, false, opcode);
- }
- void light_ctl_publish(struct bt_mesh_model *model, uint16_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("Invalid Light CTL Server state");
- 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("Invalid Light CTL Temperature Server state");
- 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("Invalid Light CTL Setup Server state");
- return;
- }
- break;
- }
- default:
- BT_ERR("Invalid Light CTL Server model 0x%04x", model->id);
- return;
- }
- send_light_ctl_status(model, NULL, true, opcode);
- }
- 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;
- uint16_t lightness = 0U, temperature = 0U;
- uint8_t tid = 0U, trans_time = 0U, delay = 0U;
- int16_t delta_uv = 0;
- bool optional = false;
- int64_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 = (int16_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("Invalid temperature 0x%04x", 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 uint8_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 uint8_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.user_data) {
- memcpy(srv->transition.timer.work.user_data, 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);
- }
- 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;
- uint16_t lightness = 0U, temperature = 0U;
- int16_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 = (int16_t) net_buf_simple_pull_le16(buf);
- if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) {
- BT_ERR("Invalid temperature 0x%04x", 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 uint8_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 uint8_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);
- }
- 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;
- uint16_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("Invalid parameter, range min 0x%04x, range max 0x%04x",
- 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 uint8_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 uint8_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);
- }
- 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;
- uint8_t tid = 0U, trans_time = 0U, delay = 0U;
- uint16_t temperature = 0U;
- int16_t delta_uv = 0;
- bool optional = false;
- int64_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 = (int16_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("Invalid temperature 0x%04x", 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 uint8_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 uint8_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.user_data) {
- memcpy(srv->transition.timer.work.user_data, 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);
- }
- /* 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, uint16_t opcode)
- {
- struct net_buf_simple *msg = NULL;
- uint8_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, Out of 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("Unknown Light HSL status opcode 0x%04x", 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));
- }
- }
- 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;
- uint16_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("Invalid Light HSL Server state");
- 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("Invalid Light HSL Hue Server state");
- 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("Invalid Light HSL Saturation Server state");
- return;
- }
- rsp_ctrl = &srv->rsp_ctrl;
- break;
- }
- default:
- BT_ERR("Invalid Light HSL Server model 0x%04x", 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("Unknown Light HSL Get opcode 0x%04x", ctx->recv_op);
- return;
- }
- send_light_hsl_status(model, ctx, false, opcode);
- }
- void light_hsl_publish(struct bt_mesh_model *model, uint16_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("Invalid Light HSL Server state");
- 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("Invalid Light HSL Hue Server state");
- 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("Invalid Light HSL Saturation Server state");
- 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("Invalid Light HSL Setup Server state");
- return;
- }
- break;
- }
- default:
- BT_ERR("Invalid Light HSL Server model 0x%04x", model->id);
- return;
- }
- send_light_hsl_status(model, NULL, true, opcode);
- }
- 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;
- uint16_t lightness = 0U, hue = 0U, saturation = 0U;
- uint8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- int64_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 uint8_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 uint8_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.user_data) {
- memcpy(srv->transition.timer.work.user_data, 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);
- }
- 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;
- uint16_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 uint8_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 uint8_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);
- }
- 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;
- uint16_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("Invalid parameter, hue min 0x%04x, hue max 0x%04x",
- hue_min, hue_max);
- return;
- }
- if (saturation_min > saturation_max) {
- BT_ERR("Invalid parameter, saturation min 0x%04x, saturation max 0x%04x",
- saturation_min, saturation_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 uint8_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 uint8_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);
- }
- 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;
- uint8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- uint16_t hue = 0U;
- int64_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 uint8_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 uint8_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.user_data) {
- memcpy(srv->transition.timer.work.user_data, 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);
- }
- 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;
- uint8_t tid = 0U, trans_time = 0U, delay = 0U;
- uint16_t saturation = 0U;
- bool optional = false;
- int64_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 uint8_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 uint8_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.user_data) {
- memcpy(srv->transition.timer.work.user_data, 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);
- }
- /* 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, uint16_t opcode)
- {
- struct net_buf_simple *msg = NULL;
- uint8_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, Out of 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("Unknown Light xyL status opcode 0x%04x", 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));
- }
- }
- 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;
- uint16_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("Unknown Light xyL Get opcode 0x%04x", ctx->recv_op);
- return;
- }
- send_light_xyl_status(model, ctx, false, opcode);
- }
- void light_xyl_publish(struct bt_mesh_model *model, uint16_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("Invalid Light xyL Server state");
- 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("Invalid Light xyL Setup Server state");
- return;
- }
- break;
- }
- default:
- BT_ERR("Invalid Light xyL Server model 0x%04x", model->id);
- return;
- }
- send_light_xyl_status(model, NULL, true, opcode);
- }
- 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;
- uint8_t tid = 0U, trans_time = 0U, delay = 0U;
- uint16_t lightness = 0U, x = 0U, y = 0U;
- bool optional = false;
- int64_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 uint8_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 uint8_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.user_data) {
- memcpy(srv->transition.timer.work.user_data, 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);
- }
- 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;
- uint16_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 uint8_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 uint8_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);
- }
- 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;
- uint16_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("Invalid parameter, x min 0x%04x, x max 0x%04x",
- x_min, x_max);
- return;
- }
- if (y_min > y_max) {
- BT_ERR("Invalid parameter, y min 0x%04x, y max 0x%04x",
- 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 uint8_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 uint8_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);
- }
- /* 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, uint16_t opcode)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- struct net_buf_simple *msg = NULL;
- uint8_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, Out of 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("Unknown Light LC status opcode 0x%04x", 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));
- }
- }
- 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;
- uint16_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("Unknown Light LC Get opcode 0x%04x", ctx->recv_op);
- return;
- }
- send_light_lc_status(model, ctx, false, opcode);
- }
- void light_lc_publish(struct bt_mesh_model *model, uint16_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);
- }
- 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;
- uint8_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("Invalid LC Mode 0x%02x", 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 uint8_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 uint8_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);
- }
- 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;
- uint8_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("Invalid LC Occupancy Mode 0x%02x", 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 uint8_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 uint8_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);
- }
- 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;
- uint8_t tid = 0U, trans_time = 0U, delay = 0U;
- bool optional = false;
- uint8_t onoff = 0U;
- int64_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 uint8_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 uint8_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.user_data) {
- memcpy(srv->transition.timer.work.user_data, 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);
- }
- 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};
- uint16_t mpid = 0U, prop_id = 0U;
- uint8_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 uint8_t *)&status, sizeof(status));
- return;
- }
- mpid = net_buf_simple_pull_le16(buf);
- if (mpid & BIT(0)) {
- length = (uint8_t)((mpid & 0xff) >> 1);
- uint8_t msb = net_buf_simple_pull_u8(buf);
- prop_id = (uint16_t)(msb << 8) | (uint16_t)(mpid >> 8);
- } else {
- length = (uint8_t)((mpid & 0x1f) >> 1);
- prop_id = (uint16_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("Invalid Motion Sensed Property length %d", length);
- return;
- }
- uint8_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 uint8_t *)&change, sizeof(change));
- }
- break;
- }
- case BLE_MESH_PEOPLE_COUNT: {
- if (length != BLE_MESH_PEOPLE_COUNT_LEN || length != buf->len) {
- BT_WARN("Invalid Motion Sensed Property length %d", length);
- return;
- }
- uint16_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 uint8_t *)&change, sizeof(change));
- }
- break;
- }
- case BLE_MESH_PRESENCE_DETECTED: {
- if (length != BLE_MESH_PRESENCE_DETECTED_LEN || length != buf->len) {
- BT_WARN("Invalid Motion Sensed Property length %d", length);
- return;
- }
- uint8_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 uint8_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("Invalid Motion Sensed Property length %d", length);
- return;
- }
- uint16_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 uint8_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("Invalid Motion Sensed Property length %d", buf->len);
- return;
- }
- uint16_t lsb = net_buf_simple_pull_le16(buf);
- uint8_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 uint8_t *)&change, sizeof(change));
- break;
- }
- default:
- break;
- }
- }
- static uint8_t *get_light_lc_prop_val(struct bt_mesh_model *model, uint16_t prop_id)
- {
- struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
- uint8_t *val = NULL;
- switch (prop_id) {
- case BLE_MESH_LIGHT_CONTROL_TIME_OCCUPANCY_DELAY:
- val = (uint8_t *)&srv->lc->prop_state.time_occupancy_delay;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_FADE_ON:
- val = (uint8_t *)&srv->lc->prop_state.time_fade_on;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_RUN_ON:
- val = (uint8_t *)&srv->lc->prop_state.time_run_on;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_FADE:
- val = (uint8_t *)&srv->lc->prop_state.time_fade;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_PROLONG:
- val = (uint8_t *)&srv->lc->prop_state.time_prolong;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_AUTO:
- val = (uint8_t *)&srv->lc->prop_state.time_fade_standby_auto;
- break;
- case BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_MANUAL:
- val = (uint8_t *)&srv->lc->prop_state.time_fade_standby_manual;
- break;
- case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_ON:
- val = (uint8_t *)&srv->lc->prop_state.lightness_on;
- break;
- case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_PROLONG:
- val = (uint8_t *)&srv->lc->prop_state.lightness_prolong;
- break;
- case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_STANDBY:
- val = (uint8_t *)&srv->lc->prop_state.lightness_standby;
- break;
- case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_ON:
- val = (uint8_t *)&srv->lc->prop_state.ambient_luxlevel_on;
- break;
- case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_PROLONG:
- val = (uint8_t *)&srv->lc->prop_state.ambient_luxlevel_prolong;
- break;
- case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_STANDBY:
- val = (uint8_t *)&srv->lc->prop_state.ambient_luxlevel_standby;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_KIU:
- val = (uint8_t *)&srv->lc->prop_state.regulator_kiu;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_KID:
- val = (uint8_t *)&srv->lc->prop_state.regulator_kid;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_KPU:
- val = (uint8_t *)&srv->lc->prop_state.regulator_kpu;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_KPD:
- val = (uint8_t *)&srv->lc->prop_state.regulator_kpd;
- break;
- case BLE_MESH_LIGHT_CONTROL_REGULATOR_ACCURACY:
- val = (uint8_t *)&srv->lc->prop_state.regulator_accuracy;
- break;
- }
- return val;
- }
- uint8_t *bt_mesh_get_lc_prop_value(struct bt_mesh_model *model, uint16_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,
- uint16_t prop_id, bool publish)
- {
- struct net_buf_simple *msg = NULL;
- uint8_t length = 1 + 2 + 4;
- uint8_t *prop_val = NULL;
- prop_val = get_light_lc_prop_val(model, prop_id);
- if (prop_val == NULL) {
- BT_ERR("Failed to get Light LC Property value");
- return;
- }
- if (publish == false) {
- msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
- if (msg == NULL) {
- BT_ERR("%s, Out of 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));
- }
- }
- 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;
- uint16_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("Invalid Light LC Property ID 0x%04x", 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 uint8_t *)&get, sizeof(get));
- return;
- }
- send_light_lc_prop_status(model, ctx, prop_id, false);
- }
- 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;
- uint8_t *prop_val = NULL, expect_len = 0U;
- uint16_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("Invalid Light LC Property ID 0x%04x", 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 uint8_t *)&set, sizeof(set));
- return;
- }
- expect_len = bt_mesh_get_dev_prop_len(prop_id);
- if (buf->len != expect_len) {
- BT_ERR("Invalid Light LC Property 0x%04x length, expect %d, actual %d",
- prop_id, expect_len, buf->len);
- return;
- }
- prop_val = get_light_lc_prop_val(model, prop_id);
- if (prop_val == NULL) {
- BT_ERR("Failed to get Light LC Property value");
- 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 uint8_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);
- }
- /* message handlers (End) */
- /* Mapping of message handlers for Light Lightness Server (0x1300) */
- const struct bt_mesh_model_op bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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 bt_mesh_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("Invalid Lighting Server user data, model id 0x%04x", 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("Invalid Light Lightness State");
- 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("Invalid Light Lightness State");
- 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("Invalid Light CTL State");
- 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("Invalid Light CTL State");
- 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("Invalid Light CTL State");
- 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("Invalid Light HSL State");
- 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("Invalid Light HSL State");
- 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("Invalid Light HSL State");
- 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("Invalid Light HSL State");
- 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("Invalid Light xyL State");
- 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("Invalid Light xyL State");
- 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("Invalid Light LC State");
- 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("Invalid Light LC State");
- return -EINVAL;
- }
- srv->model = model;
- break;
- }
- default:
- BT_WARN("Unknown Light Server, model id 0x%04x", model->id);
- return -EINVAL;
- }
- bt_mesh_mutex_create(&light_server_lock);
- return 0;
- }
- static int light_lightness_srv_init(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light Lightness Server has no publication support");
- 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("Light Lightness Setup Server not present");
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- static int light_lightness_setup_srv_init(struct bt_mesh_model *model)
- {
- return light_server_init(model);
- }
- static int light_ctl_srv_init(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light CTL Server has no publication support");
- 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("Light CTL Setup Server not present");
- /* Just give a warning here, continue with the initialization */
- }
- if (bt_mesh_elem_count() < 2) {
- BT_WARN("Light CTL Server requires two elements");
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- static int light_ctl_setup_srv_init(struct bt_mesh_model *model)
- {
- return light_server_init(model);
- }
- static int light_ctl_temp_srv_init(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light CTL Temperature Server has no publication support");
- return -EINVAL;
- }
- return light_server_init(model);
- }
- static int light_hsl_srv_init(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light HSL Server has no publication support");
- 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
- * corresponding 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("Light HSL Setup Server not present");
- /* Just give a warning here, continue with the initialization */
- }
- if (bt_mesh_elem_count() < 3) {
- BT_WARN("Light HSL Server requires three elements");
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- static int light_hsl_setup_srv_init(struct bt_mesh_model *model)
- {
- return light_server_init(model);
- }
- static int light_hsl_hue_srv_init(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light HSL Hue Server has no publication support");
- return -EINVAL;
- }
- return light_server_init(model);
- }
- static int light_hsl_sat_srv_init(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light HSL Saturation Server has no publication support");
- return -EINVAL;
- }
- return light_server_init(model);
- }
- static int light_xyl_srv_init(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light xyL Server has no publication support");
- 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("Light xyL Setup Server not present");
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- static int light_xyl_setup_srv_init(struct bt_mesh_model *model)
- {
- return light_server_init(model);
- }
- static int light_lc_srv_init(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light LC Server has no publication support");
- return -EINVAL;
- }
- return light_server_init(model);
- }
- static int light_lc_setup_srv_init(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light LC Setup Server has no publication support");
- 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("Light LC Setup Server not present");
- /* Just give a warning here, continue with the initialization */
- }
- return light_server_init(model);
- }
- #if CONFIG_BLE_MESH_DEINIT
- static int light_server_deinit(struct bt_mesh_model *model)
- {
- if (model->user_data == NULL) {
- BT_ERR("Invalid Lighting Server user data, model id 0x%04x", 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("Invalid Light Lightness State");
- 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("Invalid Light CTL State");
- 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("Invalid Light CTL State");
- 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("Invalid Light HSL State");
- 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("Invalid Light HSL State");
- 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("Invalid Light HSL State");
- 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("Invalid Light xyL State");
- 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("Invalid Light LC State");
- 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_LIGHTNESS_SETUP_SRV:
- case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV:
- case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV:
- case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV:
- case BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV:
- break;
- default:
- BT_WARN("Unknown Light Server, model id 0x%04x", model->id);
- return -EINVAL;
- }
- bt_mesh_mutex_free(&light_server_lock);
- return 0;
- }
- static int light_lightness_srv_deinit(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light Lightness Server has no publication support");
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- static int light_lightness_setup_srv_deinit(struct bt_mesh_model *model)
- {
- return light_server_deinit(model);
- }
- static int light_ctl_srv_deinit(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light CTL Server has no publication support");
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- static int light_ctl_setup_srv_deinit(struct bt_mesh_model *model)
- {
- return light_server_deinit(model);
- }
- static int light_ctl_temp_srv_deinit(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light CTL Temperature Server has no publication support");
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- static int light_hsl_srv_deinit(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light HSL Server has no publication support");
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- static int light_hsl_setup_srv_deinit(struct bt_mesh_model *model)
- {
- return light_server_deinit(model);
- }
- static int light_hsl_hue_srv_deinit(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light HSL Hue Server has no publication support");
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- static int light_hsl_sat_srv_deinit(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light HSL Saturation Server has no publication support");
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- static int light_xyl_srv_deinit(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light xyL Server has no publication support");
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- static int light_xyl_setup_srv_deinit(struct bt_mesh_model *model)
- {
- return light_server_deinit(model);
- }
- static int light_lc_srv_deinit(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light LC Server has no publication support");
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- static int light_lc_setup_srv_deinit(struct bt_mesh_model *model)
- {
- if (model->pub == NULL) {
- BT_ERR("Light LC Setup Server has no publication support");
- return -EINVAL;
- }
- return light_server_deinit(model);
- }
- #endif /* CONFIG_BLE_MESH_DEINIT */
- const struct bt_mesh_model_cb bt_mesh_light_lightness_srv_cb = {
- .init = light_lightness_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_lightness_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_lightness_setup_srv_cb = {
- .init = light_lightness_setup_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_lightness_setup_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_ctl_srv_cb = {
- .init = light_ctl_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_ctl_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_ctl_setup_srv_cb = {
- .init = light_ctl_setup_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_ctl_setup_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_ctl_temp_srv_cb = {
- .init = light_ctl_temp_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_ctl_temp_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_hsl_srv_cb = {
- .init = light_hsl_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_hsl_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_hsl_setup_srv_cb = {
- .init = light_hsl_setup_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_hsl_setup_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_hsl_hue_srv_cb = {
- .init = light_hsl_hue_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_hsl_hue_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_hsl_sat_srv_cb = {
- .init = light_hsl_sat_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_hsl_sat_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_xyl_srv_cb = {
- .init = light_xyl_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_xyl_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_xyl_setup_srv_cb = {
- .init = light_xyl_setup_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_xyl_setup_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_lc_srv_cb = {
- .init = light_lc_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_lc_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- const struct bt_mesh_model_cb bt_mesh_light_lc_setup_srv_cb = {
- .init = light_lc_setup_srv_init,
- #if CONFIG_BLE_MESH_DEINIT
- .deinit = light_lc_setup_srv_deinit,
- #endif /* CONFIG_BLE_MESH_DEINIT */
- };
- #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */
|