| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659 |
- /* 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 "model_opcode.h"
- #include "state_transition.h"
- #include "device_property.h"
- #if CONFIG_BLE_MESH_LIGHTING_SERVER
- static bt_mesh_mutex_t light_server_lock;
- static inline void bt_mesh_light_server_mutex_new(void)
- {
- if (!light_server_lock.mutex) {
- bt_mesh_mutex_create(&light_server_lock);
- }
- }
- #if CONFIG_BLE_MESH_DEINIT
- static inline void bt_mesh_light_server_mutex_free(void)
- {
- bt_mesh_mutex_free(&light_server_lock);
- }
- #endif /* CONFIG_BLE_MESH_DEINIT */
- 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));
- }
- return;
- }
- static void light_lightness_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- 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);
- return;
- }
- 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);
- return;
- }
- static void light_lightness_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- 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._reserved) {
- memcpy(srv->actual_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->actual_transition.counter == 0U) {
- srv->state->lightness_actual = srv->state->target_lightness_actual;
- /**
- * Whenever the Light Lightness Actual state is changed with a non-transactional
- * message or a completed sequence of transactional messages to a non-zero value,
- * the value of the Light Lightness Last shall be set to the value of the Light
- * Lightness Actual.
- */
- if (srv->state->lightness_actual) {
- srv->state->lightness_last = srv->state->lightness_actual;
- }
- }
- srv->actual_transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->actual_transition);
- return;
- }
- static void light_lightness_linear_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_srv *srv = model->user_data;
- 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._reserved) {
- memcpy(srv->linear_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->linear_transition.counter == 0U) {
- srv->state->lightness_linear = srv->state->target_lightness_linear;
- }
- srv->linear_transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
- send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
- }
- send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->linear_transition);
- return;
- }
- static void light_lightness_default_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- static void light_lightness_range_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- /* Light CTL Server/Temperature Server/Setup Server message handlers */
- static void send_light_ctl_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- bool publish, 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));
- }
- return;
- }
- static void light_ctl_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL;
- 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);
- return;
- }
- 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);
- return;
- }
- static void light_ctl_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_ctl_srv *srv = model->user_data;
- 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._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->lightness = srv->state->target_lightness;
- srv->state->temperature = srv->state->target_temperature;
- srv->state->delta_uv = srv->state->target_delta_uv;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_ctl_default_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- static void light_ctl_temp_range_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- static void light_ctl_temp_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
- 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._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->temperature = srv->state->target_temperature;
- srv->state->delta_uv = srv->state->target_delta_uv;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) {
- send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
- }
- send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- /* Light HSL Server/Hue Server/Saturation Server/Setup Server message handlers */
- static void send_light_hsl_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- bool publish, 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));
- }
- return;
- }
- static void light_hsl_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL;
- 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);
- return;
- }
- 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);
- return;
- }
- static void light_hsl_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_srv *srv = model->user_data;
- 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._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->lightness = srv->state->target_lightness;
- srv->state->hue = srv->state->target_hue;
- srv->state->saturation = srv->state->target_saturation;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_hsl_default_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- static void light_hsl_range_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- static void light_hsl_hue_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_hue_srv *srv = model->user_data;
- 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._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->hue = srv->state->target_hue;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_hsl_sat_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_hsl_sat_srv *srv = model->user_data;
- 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._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->saturation = srv->state->target_saturation;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) {
- send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
- }
- send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- /* Light xyL Server/Setup Server message handlers */
- static void send_light_xyl_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- bool publish, 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));
- }
- return;
- }
- static void light_xyl_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- 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);
- return;
- }
- 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);
- return;
- }
- static void light_xyl_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_xyl_srv *srv = model->user_data;
- 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._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->state->lightness = srv->state->target_lightness;
- srv->state->x = srv->state->target_x;
- srv->state->y = srv->state->target_y;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) {
- send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
- }
- send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_xyl_default_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- static void light_xyl_range_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_xyl_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- /* Light LC Server/Setup Server message handlers */
- static void send_light_lc_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- bool publish, 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));
- }
- return;
- }
- static void light_lc_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- 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);
- return;
- }
- 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);
- return;
- }
- static void light_lc_mode_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- 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);
- return;
- }
- static void light_lc_om_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- 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);
- return;
- }
- static void light_lc_light_onoff_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- 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._reserved) {
- memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
- }
- /* For Instantaneous Transition */
- if (srv->transition.counter == 0U) {
- srv->lc->state.light_onoff = srv->lc->state.target_light_onoff;
- }
- srv->transition.just_started = true;
- if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) {
- send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
- }
- send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS);
- bt_mesh_light_server_unlock();
- bt_mesh_server_start_transition(&srv->transition);
- return;
- }
- static void light_lc_sensor_status(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- /**
- * When a Light LC Server receives a Sensor Status message, and if the message
- * Raw field contains a Raw Value for the Motion Sensed Property, and the value
- * is greater than 0, or a Raw Value for the People Count Property, and the
- * value is greater than 0, or a Raw Value for the Presence Detected Property,
- * and the value is greater than 0, then it shall set the Light LC Occupancy
- * state to 0b1.
- * If the message Raw field contains a Raw Value for the Time Since Motion Sensed
- * device property, which represents a value less than or equal to the value of
- * the Light LC Occupancy Delay state, it shall delay setting the Light LC Occupancy
- * state to 0b1 by the difference between the value of the Light LC Occupancy Delay
- * state and the received Time Since Motion value.
- * When a Light LC Server receives a Sensor Status message, and if the message Raw
- * field contains a Raw Value for the Present Ambient Light Level device property,
- * it shall set the Light LC Ambient LuxLevel state to the Represented Value of the
- * received Present Ambient Light Level.
- *
- * Motion Sensed: 1 octet, 0x0042
- * People Count: 2 octets, 0x004C
- * Presence Detected: 1 octet, 0x004D
- *
- * Time Since Motion Sensed: 2 octets, 0x0068
- *
- * Present Ambient Light Level: 4 octets, 0x004E
- */
- struct bt_mesh_light_lc_srv *srv = model->user_data;
- bt_mesh_light_server_state_change_t change = {0};
- 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));
- }
- return;
- }
- static void light_lc_prop_get(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- static void light_lc_prop_set(struct bt_mesh_model *model,
- struct bt_mesh_msg_ctx *ctx,
- struct net_buf_simple *buf)
- {
- struct bt_mesh_light_lc_setup_srv *srv = model->user_data;
- 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);
- return;
- }
- /* 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_light_server_mutex_new();
- 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 corr-
- * esponding Light HSL Saturation Server model.
- */
- struct bt_mesh_elem *element = bt_mesh_model_elem(model);
- if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) == NULL) {
- BT_WARN("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_light_server_mutex_free();
- 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 */
|