nrf_cli.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701
  1. /**
  2. * Copyright (c) 2016 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_CLI)
  42. #include <ctype.h>
  43. #include <stdarg.h>
  44. #include <stdlib.h>
  45. #include "nrf_cli.h"
  46. #include "nrf_cli_vt100.h"
  47. #include "app_error.h"
  48. #include "nrf_assert.h"
  49. #include "nrf_delay.h"
  50. #include "nrf_pwr_mgmt.h"
  51. #include "nrf_atomic.h"
  52. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  53. #include "fnmatch.h"
  54. #endif
  55. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  56. #if NRF_CLI_HISTORY_ELEMENT_SIZE * NRF_CLI_HISTORY_ELEMENT_COUNT == 0
  57. #error Not proper memory size allocated for NRF_CLI_HISTORY
  58. #endif
  59. #endif
  60. /* 2 == 1 char for cmd + 1 char for '\0' */
  61. #if NRF_CLI_CMD_BUFF_SIZE < 2
  62. #error too small NRF_CLI_CMD_BUFF_SIZE
  63. #endif
  64. #if NRF_CLI_PRINTF_BUFF_SIZE < 1
  65. #error too small NRF_CLI_PRINTF_BUFF_SIZE
  66. #endif
  67. #define NRF_CLI_HELP_CLEAR "Clear screen."
  68. #define NRF_CLI_HELP_COLORS "Toggle colored syntax."
  69. #define NRF_CLI_HELP_COLORS_OFF "Disable colored syntax."
  70. #define NRF_CLI_HELP_COLORS_ON "Enable colored syntax."
  71. #define NRF_CLI_HELP_STATISTICS "CLI statistics."
  72. #define NRF_CLI_HELP_STATISTICS_SHOW "Get CLI statistics for the Logger module."
  73. #define NRF_CLI_HELP_STATISTICS_RESET "Reset CLI statistics for the Logger module."
  74. #define NRF_CLI_HELP_RESIZE "Console gets terminal screen size or assumes 80 in case " \
  75. "the readout fails. It must be executed after each terminal " \
  76. "width change to ensure correct text display."
  77. #define NRF_CLI_HELP_RESIZE_DEFAULT "Assume 80 chars screen width and send this setting " \
  78. "to the terminal."
  79. #define NRF_CLI_HELP_HISTORY "Command history."
  80. #define NRF_CLI_HELP_ECHO "Toggle CLI echo."
  81. #define NRF_CLI_HELP_ECHO_ON "Enable CLI echo."
  82. #define NRF_CLI_HELP_ECHO_OFF "Disable CLI echo. Arrows and buttons: Backspace, " \
  83. "Delete, End, Home, Insert are not handled."
  84. #define NRF_CLI_HELP_CLI "Useful, not Unix-like CLI commands."
  85. #define NRF_CLI_MSG_SPECIFY_SUBCOMMAND "Please specify a subcommand."
  86. #define NRF_CLI_MSG_UNKNOWN_PARAMETER " unknown parameter: "
  87. #define NRF_CLI_MSG_COMMAND_NOT_FOUND ": command not found"
  88. #define NRF_CLI_MSG_TAB_OVERFLOWED "Tab function: commands counter overflowed."
  89. /*lint -save -esym(526,cli_command*) -esym(526,cli_sorted_cmd_ptrs*)*/
  90. NRF_SECTION_DEF(cli_command, nrf_cli_cmd_entry_t);
  91. #define CLI_DATA_SECTION_ITEM_GET(i) NRF_SECTION_ITEM_GET(cli_command, nrf_cli_cmd_entry_t, (i))
  92. #define CLI_DATA_SECTION_ITEM_COUNT NRF_SECTION_ITEM_COUNT(cli_command, nrf_cli_cmd_entry_t)
  93. NRF_SECTION_DEF(cli_sorted_cmd_ptrs, const char *);
  94. /*lint -restore*/
  95. #define CLI_SORTED_CMD_PTRS_ITEM_GET(i) NRF_SECTION_ITEM_GET(cli_sorted_cmd_ptrs, const char *, (i))
  96. #define CLI_SORTED_CMD_PTRS_START_ADDR_GET NRF_SECTION_START_ADDR(cli_sorted_cmd_ptrs)
  97. #if defined(NRF_CLI_LOG_BACKEND) && NRF_CLI_LOG_BACKEND
  98. #include "nrf_log_str_formatter.h"
  99. #include "nrf_log_internal.h"
  100. #endif
  101. #define NRF_CLI_INIT_OPTION_PRINTER (NULL)
  102. #define NRF_CLI_MAX_TERMINAL_SIZE (250u)
  103. #define NRF_CLI_CURSOR_POSITION_BUFFER (10u) /* 10 == {esc, [, 2, 5, 0, ;, 2, 5, 0, '\0'} */
  104. #define NRF_CLI_DEFAULT_TERMINAL_WIDTH (80u) /* Default PuTTY width. */
  105. #define NRF_CLI_DEFAULT_TERMINAL_HEIGHT (24u) /* Default PuTTY height. */
  106. #define NRF_CLI_INITIAL_CURS_POS (1u) /* Initial cursor position is: (1, 1). */
  107. #define NRF_CLI_CMD_ROOT_LVL (0u)
  108. /* Macro to send VT100 commands. */
  109. #define NRF_CLI_VT100_CMD(_p_cli_, _cmd_) { \
  110. ASSERT(_p_cli_); \
  111. ASSERT(_p_cli_->p_fprintf_ctx); \
  112. static char const cmd[] = _cmd_; \
  113. nrf_fprintf(_p_cli_->p_fprintf_ctx, "%s", cmd); \
  114. }
  115. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  116. typedef enum
  117. {
  118. WILDCARD_CMD_ADDED,
  119. WILDCARD_CMD_ADDED_MISSING_SPACE,
  120. WILDCARD_CMD_NO_MATCH_FOUND
  121. } wildcard_cmd_status_t;
  122. #endif
  123. static bool cli_log_entry_process(nrf_cli_t const * p_cli, bool skip);
  124. static void cli_execute(nrf_cli_t const * p_cli);
  125. static inline void transport_buffer_flush(nrf_cli_t const * p_cli)
  126. {
  127. nrf_fprintf_buffer_flush(p_cli->p_fprintf_ctx);
  128. }
  129. static inline void cli_flag_help_set(nrf_cli_t const * p_cli)
  130. {
  131. p_cli->p_ctx->internal.flag.show_help = 1;
  132. }
  133. static inline void cli_flag_help_clear(nrf_cli_t const * p_cli)
  134. {
  135. p_cli->p_ctx->internal.flag.show_help = 0;
  136. }
  137. #if NRF_MODULE_ENABLED(NRF_CLI_BUILD_IN_CMDS)
  138. static inline void cli_flag_echo_set(nrf_cli_t const * p_cli)
  139. {
  140. p_cli->p_ctx->internal.flag.echo = 1;
  141. }
  142. static inline void cli_flag_echo_clear(nrf_cli_t const * p_cli)
  143. {
  144. p_cli->p_ctx->internal.flag.echo = 0;
  145. }
  146. #endif /* NRF_MODULE_ENABLED(NRF_CLI_BUILD_IN_CMDS) */
  147. static inline bool cli_flag_echo_is_set(nrf_cli_t const * p_cli)
  148. {
  149. return p_cli->p_ctx->internal.flag.echo == 1 ? true : false;
  150. }
  151. static inline bool cli_flag_processing_is_set(nrf_cli_t const * p_cli)
  152. {
  153. return p_cli->p_ctx->internal.flag.processing == 1 ? true : false;
  154. }
  155. static inline uint8_t cli_flag_last_nl_get(nrf_cli_t const * p_cli)
  156. {
  157. return p_cli->p_ctx->internal.flag.last_nl;
  158. }
  159. static inline void cli_flag_last_nl_set(nrf_cli_t const * p_cli, uint8_t val)
  160. {
  161. p_cli->p_ctx->internal.flag.last_nl = val;
  162. }
  163. static inline void receive_state_change(nrf_cli_t const * p_cli, nrf_cli_receive_t state)
  164. {
  165. p_cli->p_ctx->receive_state = state;
  166. }
  167. static inline size_t cli_strlen(char const * str)
  168. {
  169. return str == NULL ? 0 : strlen(str);
  170. }
  171. static void cli_cmd_buffer_clear(nrf_cli_t const * p_cli)
  172. {
  173. p_cli->p_ctx->cmd_buff[0] = '\0'; /* clear command buffer */
  174. p_cli->p_ctx->cmd_buff_pos = 0;
  175. p_cli->p_ctx->cmd_buff_len = 0;
  176. }
  177. /* Function returns true if cursor is at beginning of an empty line. */
  178. static inline bool cursor_in_empty_line(nrf_cli_t const * p_cli)
  179. {
  180. return ( (p_cli->p_ctx->cmd_buff_pos + cli_strlen(p_cli->p_name)) %
  181. p_cli->p_ctx->vt100_ctx.cons.terminal_wid == 0);
  182. }
  183. /* Function returns true if command length is equal to multiplicity of terminal width. */
  184. static inline bool full_line_cmd(nrf_cli_t const * p_cli)
  185. {
  186. return ((p_cli->p_ctx->cmd_buff_len + cli_strlen(p_cli->p_name)) %
  187. p_cli->p_ctx->vt100_ctx.cons.terminal_wid == 0);
  188. }
  189. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  190. /* Function returns true if string contains wildcard character: '?' or '*'. */
  191. static bool wildcard_character_exist(char * p_str)
  192. {
  193. size_t str_len = cli_strlen(p_str);
  194. for (size_t i = 0; i < str_len; i++)
  195. {
  196. if ((p_str[i] == '?') || (p_str[i] == '*'))
  197. {
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. #endif
  204. /* Function sends data stream to the CLI instance. Each time before the cli_write function is called,
  205. * it must be ensured that IO buffer of fprintf is flushed to avoid synchronization issues.
  206. * For that purpose, use function transport_buffer_flush(p_cli) */
  207. static void cli_write(nrf_cli_t const * p_cli,
  208. void const * p_data,
  209. size_t length,
  210. size_t * p_cnt)
  211. {
  212. ASSERT(p_cli && p_data);
  213. ASSERT(p_cli->p_iface->p_api);
  214. size_t offset = 0;
  215. size_t cnt;
  216. while (length)
  217. {
  218. ret_code_t ret = p_cli->p_iface->p_api->write(p_cli->p_iface,
  219. &((uint8_t const *)p_data)[offset],
  220. length,
  221. &cnt);
  222. UNUSED_VARIABLE(ret);
  223. ASSERT(ret == NRF_SUCCESS);
  224. ASSERT(length >= cnt);
  225. offset += cnt;
  226. length -= cnt;
  227. if (cnt == 0 && (p_cli->p_ctx->state != NRF_CLI_STATE_PANIC_MODE_ACTIVE))
  228. {
  229. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  230. (void)task_events_wait(NRF_CLI_TRANSPORT_TX_RDY_TASK_EVT);
  231. #else
  232. while (p_cli->p_ctx->internal.flag.tx_rdy == 0)
  233. {
  234. ;
  235. }
  236. p_cli->p_ctx->internal.flag.tx_rdy = 0;
  237. #endif
  238. }
  239. }
  240. if (p_cnt)
  241. {
  242. *p_cnt = cnt;
  243. }
  244. }
  245. /* Function sends 1 character to the CLI instance. */
  246. static inline void cli_putc(nrf_cli_t const * p_cli, char ch)
  247. {
  248. nrf_fprintf(p_cli->p_fprintf_ctx, "%c", ch);
  249. }
  250. /* Function reads data from the CLI instance. */
  251. static void cli_read(nrf_cli_t const * p_cli,
  252. void * p_data,
  253. size_t length,
  254. size_t * p_cnt)
  255. {
  256. ASSERT(p_cli && p_data);
  257. ASSERT(p_cli->p_iface);
  258. ret_code_t ret = p_cli->p_iface->p_api->read(p_cli->p_iface, p_data, length, p_cnt);
  259. UNUSED_VARIABLE(ret);
  260. }
  261. /* Function cmd_get shall be used to search commands. It moves the pointer pp_entry to command
  262. * of static command structure. If the command cannot be found, the function will set pp_entry to NULL.
  263. * p_command Pointer to command which will be processed (no matter the root command).
  264. * lvl Level of the requested command.
  265. * idx Index of the requested command.
  266. * pp_entry Pointer which points to subcommand[idx] after function execution.
  267. * p_st_entry Pointer to the structure where dynamic entry data can be stored.
  268. */
  269. static void cmd_get(nrf_cli_cmd_entry_t const * p_command,
  270. size_t lvl,
  271. size_t idx,
  272. nrf_cli_static_entry_t const ** pp_entry,
  273. nrf_cli_static_entry_t * p_st_entry)
  274. {
  275. ASSERT (pp_entry != NULL);
  276. ASSERT (p_st_entry != NULL);
  277. if (lvl == NRF_CLI_CMD_ROOT_LVL)
  278. {
  279. if (idx < CLI_DATA_SECTION_ITEM_COUNT)
  280. {
  281. nrf_cli_cmd_entry_t const * p_cmd = NULL;
  282. char const * * pp_sorted_cmds = (char const * *)CLI_SORTED_CMD_PTRS_START_ADDR_GET;
  283. for (size_t i = 0; i < CLI_DATA_SECTION_ITEM_COUNT; i++)
  284. {
  285. p_cmd = CLI_DATA_SECTION_ITEM_GET(i);
  286. if (!strcmp(pp_sorted_cmds[idx], p_cmd->u.p_static->p_syntax))
  287. {
  288. *pp_entry = p_cmd->u.p_static;
  289. return;
  290. }
  291. }
  292. }
  293. *pp_entry = NULL;
  294. return;
  295. }
  296. if (p_command == NULL)
  297. {
  298. *pp_entry = NULL;
  299. return;
  300. }
  301. if (p_command->is_dynamic)
  302. {
  303. p_command->u.p_dynamic_get(idx, p_st_entry);
  304. if (p_st_entry->p_syntax == NULL)
  305. {
  306. *pp_entry = NULL;
  307. }
  308. else
  309. {
  310. *pp_entry = p_st_entry;
  311. }
  312. }
  313. else
  314. {
  315. if (p_command->u.p_static[idx].p_syntax != NULL)
  316. {
  317. *pp_entry = &p_command->u.p_static[idx];
  318. }
  319. else
  320. {
  321. *pp_entry = NULL;
  322. }
  323. }
  324. }
  325. /* Function multiline_console_data_check checks the current cursor position (x, y) on terminal screen
  326. * based on: command length, console name length, and terminal width.
  327. * Example 1:
  328. * || - cursor
  329. * ----------------------------
  330. * |console_name $: || |
  331. * ----------------------------
  332. * => coordinates are: cur_x = 17, cur_x_end = 17,
  333. * cur_y = 1, cur_y_end = 1
  334. * Example 2:
  335. * ----------------------------
  336. * |console_name $: test command|
  337. * |showing |e|xample |
  338. * ----------------------------
  339. * => coordinates are: cur_x = 9, cur_x_end = 18 (cursor can be one column after 'e')
  340. * => cur_y = 2, cur_y_end = 2
  341. * Example 3:
  342. * ----------------------------
  343. * |console_name $: test command|
  344. * |showing e|x|ample with more |
  345. * |parameters |
  346. * ----------------------------
  347. * => coordinates are: cur_x = 10, cur_x_end = 11 (cursor can be one column after 's')
  348. * => cur_y = 2, cur_y_end = 3
  349. */
  350. static nrf_cli_multiline_cons_t const * multiline_console_data_check(nrf_cli_t const * p_cli)
  351. {
  352. nrf_cli_ctx_t * p_ctx = p_cli->p_ctx;
  353. nrf_cli_multiline_cons_t * p_cons = &p_cli->p_ctx->vt100_ctx.cons;
  354. p_cons->name_len = cli_strlen(p_cli->p_name);
  355. /* Current cursor position in command.
  356. * +1 -> because home position is (1, 1) */
  357. p_cons->cur_x = (p_ctx->cmd_buff_pos + p_cons->name_len) % p_cons->terminal_wid + 1;
  358. p_cons->cur_y = (p_ctx->cmd_buff_pos + p_cons->name_len) / p_cons->terminal_wid + 1;
  359. /* Extreme position when cursor is at the end of command. */
  360. p_cons->cur_y_end = (p_ctx->cmd_buff_len + p_cons->name_len) / p_cons->terminal_wid + 1;
  361. p_cons->cur_x_end = (p_ctx->cmd_buff_len + p_cons->name_len) % p_cons->terminal_wid + 1;
  362. return p_cons;
  363. }
  364. #if NRF_MODULE_ENABLED(NRF_CLI_METAKEYS)
  365. /* Calculates relative line number of given position in buffer */
  366. static uint32_t cli_line_num_with_buffer_offset_get(nrf_cli_t const * p_cli,
  367. nrf_cli_cmd_len_t buffer_pos)
  368. {
  369. uint32_t name_len;
  370. nrf_cli_multiline_cons_t *p_cons = &p_cli->p_ctx->vt100_ctx.cons;
  371. name_len = cli_strlen(p_cli->p_name);
  372. return ((buffer_pos + name_len) / p_cons->terminal_wid);
  373. }
  374. #endif
  375. #if NRF_MODULE_ENABLED(NRF_CLI_METAKEYS)
  376. /* Calculates column number of given position in buffer */
  377. static uint32_t cli_col_num_with_buffer_offset_get(nrf_cli_t const * p_cli,
  378. nrf_cli_cmd_len_t buffer_pos)
  379. {
  380. uint32_t name_len;
  381. nrf_cli_multiline_cons_t *p_cons = &p_cli->p_ctx->vt100_ctx.cons;
  382. name_len = cli_strlen(p_cli->p_name);
  383. /* columns are counted from 1 */
  384. return (1 + ((buffer_pos + name_len) % p_cons->terminal_wid));
  385. }
  386. #endif
  387. #if NRF_MODULE_ENABLED(NRF_CLI_METAKEYS)
  388. /* For the given buffer, calculates row span between position2 and position1 */
  389. static int32_t cli_row_span_with_buffer_offsets_get(nrf_cli_t const * p_cli,
  390. nrf_cli_cmd_len_t offset1,
  391. nrf_cli_cmd_len_t offset2)
  392. {
  393. return cli_line_num_with_buffer_offset_get(p_cli, offset2)
  394. - cli_line_num_with_buffer_offset_get(p_cli, offset1);
  395. }
  396. #endif
  397. #if NRF_MODULE_ENABLED(NRF_CLI_METAKEYS)
  398. /* For the given buffer, calculates column span between position2 and position 1 */
  399. static int32_t cli_column_span_with_buffer_offsets_get(nrf_cli_t const * p_cli,
  400. nrf_cli_cmd_len_t offset1,
  401. nrf_cli_cmd_len_t offset2)
  402. {
  403. return cli_col_num_with_buffer_offset_get(p_cli, offset2)
  404. - cli_col_num_with_buffer_offset_get(p_cli, offset1);
  405. }
  406. #endif
  407. /* Function sends VT100 command to clear the screen from cursor position to end of the screen. */
  408. static inline void cli_clear_eos(nrf_cli_t const * p_cli)
  409. {
  410. NRF_CLI_VT100_CMD(p_cli, NRF_CLI_VT100_CLEAREOS);
  411. }
  412. /* Function sends VT100 command to save cursor position. */
  413. static inline void cli_cursor_save(nrf_cli_t const * p_cli)
  414. {
  415. NRF_CLI_VT100_CMD(p_cli, NRF_CLI_VT100_SAVECURSOR);
  416. }
  417. /* Function sends VT100 command to restore saved cursor position. */
  418. static inline void cli_cursor_restore(nrf_cli_t const * p_cli)
  419. {
  420. NRF_CLI_VT100_CMD(p_cli, NRF_CLI_VT100_RESTORECURSOR);
  421. }
  422. /* Function forcing new line - cannot be replaced with function cursor_down_move. */
  423. static inline void cursor_next_line_move(nrf_cli_t const * p_cli)
  424. {
  425. nrf_fprintf(p_cli->p_fprintf_ctx, "\n");
  426. }
  427. /* Function moves cursor left by n positions. */
  428. static inline void cursor_left_move(nrf_cli_t const * p_cli, nrf_cli_cmd_len_t n)
  429. {
  430. if (n > 0)
  431. {
  432. nrf_fprintf(p_cli->p_fprintf_ctx, "\033[%dD", n);
  433. }
  434. }
  435. /* Function moves cursor right by n positions. */
  436. static inline void cursor_right_move(nrf_cli_t const * p_cli, nrf_cli_cmd_len_t n)
  437. {
  438. if (n > 0)
  439. {
  440. nrf_fprintf(p_cli->p_fprintf_ctx, "\033[%dC", n);
  441. }
  442. }
  443. #if NRF_MODULE_ENABLED(NRF_CLI_METAKEYS)
  444. /* Moves cursor horizontally by a number. Positive is right */
  445. static void cursor_horiz_move(nrf_cli_t const * p_cli, int32_t delta)
  446. {
  447. if (delta > 0)
  448. {
  449. cursor_right_move(p_cli, delta);
  450. }
  451. else if (delta < 0)
  452. {
  453. cursor_left_move(p_cli, -delta);
  454. }
  455. else { }
  456. }
  457. #endif
  458. /* Function moves cursor up by n positions. */
  459. static inline void cursor_up_move(nrf_cli_t const * p_cli, nrf_cli_cmd_len_t n)
  460. {
  461. if (n > 0)
  462. {
  463. nrf_fprintf(p_cli->p_fprintf_ctx, "\033[%dA", n);
  464. }
  465. }
  466. /* Function moves cursor down by n positions but it will bring no effect if cursor is in the last
  467. * line of terminal screen. In such case, the cursor_next_line_move function shall be invoked. */
  468. static inline void cursor_down_move(nrf_cli_t const * p_cli, nrf_cli_cmd_len_t n)
  469. {
  470. if (n > 0)
  471. {
  472. nrf_fprintf(p_cli->p_fprintf_ctx, "\033[%dB", n);
  473. }
  474. }
  475. #if NRF_MODULE_ENABLED(NRF_CLI_METAKEYS)
  476. /* Moves cursor vertically by a number. Positive is down */
  477. static void cursor_vert_move(nrf_cli_t const * p_cli, int32_t delta)
  478. {
  479. if (delta > 0)
  480. {
  481. cursor_down_move(p_cli, delta);
  482. }
  483. else if (delta < 0)
  484. {
  485. cursor_up_move(p_cli, -delta);
  486. }
  487. else { }
  488. }
  489. #endif
  490. /* Function increments cursor position (if possible) and moves cursor to new line if necessary. */
  491. static void cursor_position_increment(nrf_cli_t const * p_cli)
  492. {
  493. if (p_cli->p_ctx->cmd_buff_pos >= p_cli->p_ctx->cmd_buff_len)
  494. {
  495. return; /* incrementation not possible */
  496. }
  497. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  498. nrf_cli_cmd_len_t cur_y = p_cons->cur_y;
  499. ++p_cli->p_ctx->cmd_buff_pos;
  500. p_cons = multiline_console_data_check(p_cli);
  501. if (cur_y == p_cons->cur_y)
  502. {
  503. cursor_right_move(p_cli, 1);
  504. }
  505. else
  506. {
  507. cursor_next_line_move(p_cli);
  508. }
  509. }
  510. /* Function will move cursor back to position == cmd_buff_pos. Example usage is when cursor needs
  511. * to be moved back after printing some text. This function cannot be used to move cursor to new
  512. * location by manual change of cmd_buff_pos.*/
  513. static void cursor_position_synchronize(nrf_cli_t const * p_cli)
  514. {
  515. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  516. bool last_line = p_cons->cur_y == p_cons->cur_y_end ? true : false;
  517. /* In case cursor reaches the bottom line of a terminal, it will be moved to the next line. */
  518. if (cursor_in_empty_line(p_cli) || full_line_cmd(p_cli))
  519. {
  520. cursor_next_line_move(p_cli);
  521. }
  522. if (last_line)
  523. {
  524. cursor_left_move(p_cli, p_cons->cur_x_end - p_cons->cur_x);
  525. }
  526. else
  527. {
  528. cursor_up_move(p_cli, p_cons->cur_y_end - p_cons->cur_y);
  529. if (p_cons->cur_x > p_cons->cur_x_end)
  530. {
  531. cursor_right_move(p_cli, p_cons->cur_x - p_cons->cur_x_end);
  532. }
  533. else
  534. {
  535. cursor_left_move(p_cli, p_cons->cur_x_end - p_cons->cur_x);
  536. }
  537. }
  538. }
  539. #if NRF_MODULE_ENABLED(NRF_CLI_METAKEYS)
  540. /**
  541. * Removes the "word" to the left of the cursor:
  542. * - if there are spaces at the cursor position, remove all spaces to the left
  543. * - remove the non-spaces (word) until a space is found or a beginning of buffer
  544. */
  545. static void cli_cmd_word_remove(nrf_cli_t const * p_cli)
  546. {
  547. nrf_cli_cmd_len_t new_pos;
  548. nrf_cli_cmd_len_t chars_to_delete;
  549. int32_t row_span;
  550. int32_t col_span;
  551. /* Line must not be empty and cursor must not be at 0 to continue */
  552. if ((p_cli->p_ctx->cmd_buff_len == 0) || (p_cli->p_ctx->cmd_buff_pos == 0))
  553. {
  554. return;
  555. }
  556. /* start at the current position */
  557. new_pos = p_cli->p_ctx->cmd_buff_pos;
  558. chars_to_delete = 0;
  559. /* look back for all spaces then for non-spaces */
  560. while ((new_pos >= 1) && (p_cli->p_ctx->cmd_buff[new_pos - 1] == ' '))
  561. {
  562. ++chars_to_delete;
  563. --new_pos;
  564. }
  565. while ((new_pos >= 1) && (p_cli->p_ctx->cmd_buff[new_pos - 1] != ' '))
  566. {
  567. --new_pos;
  568. ++chars_to_delete;
  569. }
  570. /* calculate the new cursor */
  571. row_span = cli_row_span_with_buffer_offsets_get(p_cli, p_cli->p_ctx->cmd_buff_pos, new_pos);
  572. col_span = cli_column_span_with_buffer_offsets_get(p_cli, p_cli->p_ctx->cmd_buff_pos, new_pos);
  573. /* manage the buffer */
  574. memmove(&p_cli->p_ctx->cmd_buff[new_pos],
  575. &p_cli->p_ctx->cmd_buff[new_pos + chars_to_delete],
  576. p_cli->p_ctx->cmd_buff_len - chars_to_delete);
  577. p_cli->p_ctx->cmd_buff_len -= chars_to_delete;
  578. p_cli->p_ctx->cmd_buff_pos = new_pos;
  579. p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_len] = '\0';
  580. /* update display */
  581. cursor_horiz_move(p_cli, col_span);
  582. cursor_vert_move(p_cli, row_span);
  583. cli_cursor_save(p_cli);
  584. nrf_cli_fprintf(p_cli,
  585. NRF_CLI_NORMAL,
  586. "%s",
  587. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos]);
  588. cli_clear_eos(p_cli);
  589. cli_cursor_restore(p_cli);
  590. }
  591. #endif
  592. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY) || NRF_MODULE_ENABLED(NRF_CLI_METAKEYS)
  593. /* Function moves cursor to begin of command position, just after console name. */
  594. static void cursor_home_position_move(nrf_cli_t const * p_cli)
  595. {
  596. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  597. if ((p_cons->cur_x == p_cons->name_len + NRF_CLI_INITIAL_CURS_POS) &&
  598. (p_cons->cur_y == NRF_CLI_INITIAL_CURS_POS))
  599. {
  600. return; /* nothing to handle because cursor is in start position */
  601. }
  602. if (p_cons->cur_y > NRF_CLI_INITIAL_CURS_POS)
  603. {
  604. cursor_up_move(p_cli, p_cons->cur_y - NRF_CLI_INITIAL_CURS_POS);
  605. }
  606. if (p_cons->cur_x > p_cons->name_len)
  607. {
  608. cursor_left_move(p_cli, p_cons->cur_x - NRF_CLI_INITIAL_CURS_POS - p_cons->name_len);
  609. }
  610. else
  611. {
  612. cursor_right_move(p_cli, p_cons->name_len + NRF_CLI_INITIAL_CURS_POS - p_cons->cur_x);
  613. }
  614. /* align data buffer pointer with cursor position */
  615. p_cli->p_ctx->cmd_buff_pos = 0;
  616. }
  617. #endif
  618. /* Function moves cursor to end of command. */
  619. static void cursor_end_position_move(nrf_cli_t const * p_cli)
  620. {
  621. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  622. if ((p_cons->cur_x == p_cons->cur_x_end) && (p_cons->cur_y == p_cons->cur_y_end))
  623. {
  624. return; /* nothing to handle because cursor is in end position */
  625. }
  626. if (p_cons->cur_y_end > p_cons->cur_y)
  627. {
  628. cursor_down_move(p_cli, p_cons->cur_y_end - p_cons->cur_y);
  629. }
  630. if (p_cons->cur_x > p_cons->cur_x_end)
  631. {
  632. cursor_left_move(p_cli, p_cons->cur_x - p_cons->cur_x_end);
  633. }
  634. else
  635. {
  636. cursor_right_move(p_cli, p_cons->cur_x_end - p_cons->cur_x);
  637. }
  638. /* align data buffer pointer with cursor position */
  639. p_cli->p_ctx->cmd_buff_pos = p_cli->p_ctx->cmd_buff_len;
  640. }
  641. #if NRF_MODULE_ENABLED(NRF_CLI_BUILD_IN_CMDS)
  642. /* Function reads cursor position from terminal. */
  643. static ret_code_t cursor_position_get(nrf_cli_t const * p_cli)
  644. {
  645. size_t cnt;
  646. uint16_t x = 0; /* horizontal position */
  647. uint16_t y = 0; /* vertical position */
  648. char c = 0;
  649. nrf_cli_cmd_len_t buff_idx = 0;
  650. /* clear temp buffer */
  651. memset(p_cli->p_ctx->temp_buff, 0, sizeof(p_cli->p_ctx->temp_buff));
  652. /* escape code asking terminal about its size */
  653. static char const cmd_get_terminal_size[] = "\033[6n";
  654. nrf_fprintf(p_cli->p_fprintf_ctx, cmd_get_terminal_size);
  655. /* fprintf buffer needs to be flushed to start sending prepared escape code to the terminal */
  656. transport_buffer_flush(p_cli);
  657. /* timeout for terminal response = ~1s */
  658. for (uint16_t i = 0; i < 1000; i++)
  659. {
  660. do
  661. {
  662. cli_read(p_cli, &c, sizeof(c), &cnt);
  663. if (cnt == 0)
  664. {
  665. nrf_delay_us(999);
  666. continue;
  667. }
  668. if ((c != NRF_CLI_VT100_ASCII_ESC) &&
  669. (p_cli->p_ctx->temp_buff[0] != NRF_CLI_VT100_ASCII_ESC))
  670. {
  671. continue;
  672. }
  673. if (c == 'R') /* end of response from the terminal */
  674. {
  675. p_cli->p_ctx->temp_buff[buff_idx] = '\0';
  676. if (p_cli->p_ctx->temp_buff[1] != '[')
  677. {
  678. p_cli->p_ctx->temp_buff[0] = 0;
  679. return NRF_ERROR_INVALID_DATA;
  680. }
  681. buff_idx = 2; /* index start position in the buffer where 'y' is stored */
  682. while (p_cli->p_ctx->temp_buff[buff_idx] != ';')
  683. {
  684. y = y * 10 + (p_cli->p_ctx->temp_buff[buff_idx++] - '0');
  685. if (buff_idx >= NRF_CLI_CMD_BUFF_SIZE)
  686. {
  687. return NRF_ERROR_DATA_SIZE;
  688. }
  689. }
  690. if (++buff_idx >= NRF_CLI_CMD_BUFF_SIZE)
  691. {
  692. return NRF_ERROR_DATA_SIZE;
  693. }
  694. while (p_cli->p_ctx->temp_buff[buff_idx] != '\0')
  695. {
  696. x = x * 10 + (p_cli->p_ctx->temp_buff[buff_idx++] - '0');
  697. if (buff_idx >= NRF_CLI_CMD_BUFF_SIZE)
  698. {
  699. return NRF_ERROR_DATA_SIZE;
  700. }
  701. }
  702. /* horizontal cursor position */
  703. if (x > NRF_CLI_MAX_TERMINAL_SIZE)
  704. {
  705. p_cli->p_ctx->vt100_ctx.cons.cur_x = NRF_CLI_MAX_TERMINAL_SIZE;
  706. }
  707. else
  708. {
  709. p_cli->p_ctx->vt100_ctx.cons.cur_x = (nrf_cli_cmd_len_t)x;
  710. }
  711. /* vertical cursor position */
  712. if (y > NRF_CLI_MAX_TERMINAL_SIZE)
  713. {
  714. p_cli->p_ctx->vt100_ctx.cons.cur_y = NRF_CLI_MAX_TERMINAL_SIZE;
  715. }
  716. else
  717. {
  718. p_cli->p_ctx->vt100_ctx.cons.cur_y = (nrf_cli_cmd_len_t)y;
  719. }
  720. p_cli->p_ctx->temp_buff[0] = 0;
  721. return NRF_SUCCESS;
  722. }
  723. else
  724. {
  725. p_cli->p_ctx->temp_buff[buff_idx] = c;
  726. }
  727. if (++buff_idx > NRF_CLI_CURSOR_POSITION_BUFFER - 1)
  728. {
  729. p_cli->p_ctx->temp_buff[0] = 0;
  730. /* data_buf[NRF_CLI_CURSOR_POSITION_BUFFER - 1] is reserved for '\0' */
  731. return NRF_ERROR_NO_MEM;
  732. }
  733. } while (cnt > 0);
  734. }
  735. return NRF_ERROR_TIMEOUT;
  736. }
  737. /* Function gets terminal width and height. */
  738. static ret_code_t terminal_size_get(nrf_cli_t const * p_cli,
  739. nrf_cli_cmd_len_t * p_length,
  740. nrf_cli_cmd_len_t * p_height)
  741. {
  742. ASSERT(p_length);
  743. ASSERT(p_height);
  744. uint16_t x;
  745. uint16_t y;
  746. if (cursor_position_get(p_cli) == NRF_SUCCESS)
  747. {
  748. x = p_cli->p_ctx->vt100_ctx.cons.cur_x;
  749. y = p_cli->p_ctx->vt100_ctx.cons.cur_y;
  750. /* assumption: terminal widht and height < 999 */
  751. cursor_right_move(p_cli, NRF_CLI_MAX_TERMINAL_SIZE); /* move to last column */
  752. cursor_down_move(p_cli, NRF_CLI_MAX_TERMINAL_SIZE); /* move to last row */
  753. }
  754. else
  755. {
  756. return NRF_ERROR_NOT_SUPPORTED;
  757. }
  758. if (cursor_position_get(p_cli) == NRF_SUCCESS)
  759. {
  760. *p_length = p_cli->p_ctx->vt100_ctx.cons.cur_x;
  761. *p_height = p_cli->p_ctx->vt100_ctx.cons.cur_y;
  762. cursor_left_move(p_cli, *p_length - x);
  763. cursor_up_move(p_cli, *p_height - y);
  764. return NRF_SUCCESS;
  765. }
  766. return NRF_ERROR_NOT_SUPPORTED;
  767. }
  768. #endif // NRF_MODULE_ENABLED(NRF_CLI_BUILD_IN_CMDS)
  769. #if NRF_MODULE_ENABLED(NRF_CLI_VT100_COLORS)
  770. static void vt100_color_set(nrf_cli_t const * p_cli, nrf_cli_vt100_color_t color)
  771. {
  772. if (color != NRF_CLI_DEFAULT)
  773. {
  774. if (p_cli->p_ctx->vt100_ctx.col.col == color)
  775. {
  776. return;
  777. }
  778. uint8_t cmd[] = NRF_CLI_VT100_COLOR(color - 1);
  779. p_cli->p_ctx->vt100_ctx.col.col = color;
  780. nrf_fprintf(p_cli->p_fprintf_ctx, "%s", cmd);
  781. }
  782. else
  783. {
  784. static uint8_t const cmd[] = NRF_CLI_VT100_MODESOFF;
  785. p_cli->p_ctx->vt100_ctx.col.col = color;
  786. nrf_fprintf(p_cli->p_fprintf_ctx, "%s", cmd);
  787. }
  788. }
  789. static void vt100_bgcolor_set(nrf_cli_t const * p_cli, nrf_cli_vt100_color_t bgcolor)
  790. {
  791. if (bgcolor != NRF_CLI_DEFAULT)
  792. {
  793. if (p_cli->p_ctx->vt100_ctx.col.bgcol == bgcolor)
  794. {
  795. return;
  796. }
  797. /* -1 because default value is first in enum */
  798. uint8_t cmd[] = NRF_CLI_VT100_BGCOLOR(bgcolor - 1);
  799. p_cli->p_ctx->vt100_ctx.col.bgcol = bgcolor;
  800. nrf_fprintf(p_cli->p_fprintf_ctx, "%s", cmd);
  801. }
  802. }
  803. static inline void vt100_colors_store(nrf_cli_t const * p_cli,
  804. nrf_cli_vt100_colors_t * p_color)
  805. {
  806. memcpy(p_color, &p_cli->p_ctx->vt100_ctx.col, sizeof(nrf_cli_vt100_colors_t));
  807. }
  808. static void vt100_colors_restore(nrf_cli_t const * p_cli,
  809. nrf_cli_vt100_colors_t const * p_color)
  810. {
  811. vt100_color_set(p_cli, p_color->col);
  812. vt100_bgcolor_set(p_cli, p_color->bgcol);
  813. }
  814. #endif // NRF_MODULE_ENABLED(NRF_CLI_VT100_COLORS)
  815. static void left_arrow_handle(nrf_cli_t const * p_cli)
  816. {
  817. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  818. if ((p_cons->cur_x == p_cons->name_len + NRF_CLI_INITIAL_CURS_POS) &&
  819. (p_cons->cur_y == NRF_CLI_INITIAL_CURS_POS))
  820. {
  821. return; /* nothing to handle because cursor is in start position */
  822. }
  823. if (p_cons->cur_x == NRF_CLI_INITIAL_CURS_POS)
  824. { /* go to previous line */
  825. cursor_up_move(p_cli, 1);
  826. cursor_right_move(p_cli, p_cons->terminal_wid);
  827. --p_cli->p_ctx->cmd_buff_pos;
  828. }
  829. else
  830. {
  831. cursor_left_move(p_cli, 1);
  832. --p_cli->p_ctx->cmd_buff_pos;
  833. }
  834. }
  835. static void right_arrow_handle(nrf_cli_t const * p_cli)
  836. {
  837. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  838. if ((p_cons->cur_x == p_cons->cur_x_end) &&
  839. (p_cons->cur_y == p_cons->cur_y_end))
  840. {
  841. return; /* nothing to handle because cursor is in start position */
  842. }
  843. if (p_cons->cur_x == p_cons->terminal_wid) /* go to next line */
  844. {
  845. cursor_down_move(p_cli, 1);
  846. cursor_left_move(p_cli, p_cons->terminal_wid);
  847. ++p_cli->p_ctx->cmd_buff_pos;
  848. }
  849. else
  850. {
  851. cursor_right_move(p_cli, 1);
  852. ++p_cli->p_ctx->cmd_buff_pos;
  853. }
  854. }
  855. static inline void char_insert_echo_off(nrf_cli_t const * p_cli, char data)
  856. {
  857. if (p_cli->p_ctx->cmd_buff_len >= (NRF_CLI_CMD_BUFF_SIZE - 1))
  858. {
  859. return;
  860. }
  861. p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos++] = data;
  862. p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos] = '\0';
  863. ++p_cli->p_ctx->cmd_buff_len;
  864. }
  865. static void char_insert(nrf_cli_t const * p_cli, char data)
  866. {
  867. nrf_cli_cmd_len_t diff;
  868. bool ins_mode = (bool)p_cli->p_ctx->internal.flag.insert_mode;
  869. diff = p_cli->p_ctx->cmd_buff_len - p_cli->p_ctx->cmd_buff_pos;
  870. if (!ins_mode)
  871. {
  872. if (p_cli->p_ctx->cmd_buff_len >= (NRF_CLI_CMD_BUFF_SIZE - 1))
  873. {
  874. return;
  875. }
  876. if (diff > 0)
  877. {
  878. memmove(&p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos + 1],
  879. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos],
  880. diff);
  881. }
  882. }
  883. else
  884. {
  885. if ((p_cli->p_ctx->cmd_buff_len >= (NRF_CLI_CMD_BUFF_SIZE - 1)) &&
  886. (diff == 0))
  887. {
  888. /* If cmd buffer is full, it is possible to replace chars but adding new
  889. is not allowed. */
  890. return;
  891. }
  892. }
  893. p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos] = data;
  894. if (!ins_mode)
  895. {
  896. p_cli->p_ctx->cmd_buff[++p_cli->p_ctx->cmd_buff_len] = '\0';
  897. }
  898. if (diff > 0)
  899. {
  900. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  901. bool last_line = p_cons->cur_y == p_cons->cur_y_end ? true : false;
  902. /* Below if-else statement is to minimize esc codes transmission. */
  903. if (last_line)
  904. {
  905. nrf_cli_fprintf(p_cli,
  906. NRF_CLI_NORMAL,
  907. "%s",
  908. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos]);
  909. /* Move cursor one position left less in case of insert mode. */
  910. cursor_left_move(p_cli, diff - ins_mode);
  911. }
  912. else
  913. {
  914. /* Save the current cursor position in order to get back after fprintf function. */
  915. cli_cursor_save(p_cli);
  916. nrf_cli_fprintf(p_cli,
  917. NRF_CLI_NORMAL,
  918. "%s",
  919. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos]);
  920. cli_cursor_restore(p_cli);
  921. /* Move cursor right by one position to edit the next character. */
  922. cursor_right_move(p_cli, 1);
  923. }
  924. }
  925. else
  926. {
  927. /* New char appended at the end of buffer. */
  928. if (ins_mode)
  929. {
  930. p_cli->p_ctx->cmd_buff[++p_cli->p_ctx->cmd_buff_len] = '\0';
  931. }
  932. cli_putc(p_cli, data);
  933. }
  934. /* Incrementation needs to be executed before invoking function: cursor_in_empty_line. */
  935. ++p_cli->p_ctx->cmd_buff_pos;
  936. /* Forcing terminal to switch to a new line if the command is too long. */
  937. if (cursor_in_empty_line(p_cli))
  938. {
  939. cursor_next_line_move(p_cli);
  940. return;
  941. }
  942. if (full_line_cmd(p_cli))
  943. {
  944. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  945. /* The code below will force the terminal to scroll one line down when the currently entered command
  946. * reaches lower right corner of the terminal screen. */
  947. cursor_down_move(p_cli, p_cons->cur_y_end - p_cons->cur_y - 1);
  948. cursor_next_line_move(p_cli);
  949. cursor_up_move(p_cli, p_cons->cur_y_end - p_cons->cur_y);
  950. cursor_right_move(p_cli, p_cons->cur_x - 1);
  951. return;
  952. }
  953. }
  954. static void char_backspace(nrf_cli_t const * p_cli)
  955. {
  956. nrf_cli_cmd_len_t diff;
  957. if ((p_cli->p_ctx->cmd_buff_len == 0) || (p_cli->p_ctx->cmd_buff_pos == 0))
  958. {
  959. return;
  960. }
  961. diff = p_cli->p_ctx->cmd_buff_len - p_cli->p_ctx->cmd_buff_pos;
  962. memmove(&p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos - 1],
  963. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos],
  964. diff + 1);
  965. --p_cli->p_ctx->cmd_buff_pos;
  966. --p_cli->p_ctx->cmd_buff_len;
  967. if (diff > 0)
  968. {
  969. cli_putc(p_cli, NRF_CLI_VT100_ASCII_BSPACE);
  970. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  971. bool last_line = p_cons->cur_y == p_cons->cur_y_end ? true : false;
  972. if (last_line)
  973. {
  974. nrf_cli_fprintf(p_cli,
  975. NRF_CLI_NORMAL,
  976. "%s",
  977. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos]);
  978. cli_clear_eos(p_cli);
  979. cursor_left_move(p_cli, diff);
  980. }
  981. else
  982. {
  983. /* If cursor is not in last cmd line, its position needs to be saved by
  984. * VT100 command. */
  985. cli_cursor_save(p_cli);
  986. cli_clear_eos(p_cli);
  987. nrf_cli_fprintf(p_cli,
  988. NRF_CLI_NORMAL,
  989. "%s",
  990. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos]);
  991. cli_cursor_restore(p_cli);
  992. }
  993. }
  994. else
  995. {
  996. static char const cmd_bspace[] = {
  997. NRF_CLI_VT100_ASCII_BSPACE, ' ', NRF_CLI_VT100_ASCII_BSPACE, '\0'};
  998. nrf_fprintf(p_cli->p_fprintf_ctx, "%s", cmd_bspace);
  999. }
  1000. }
  1001. static void char_delete(nrf_cli_t const * p_cli)
  1002. {
  1003. nrf_cli_cmd_len_t diff;
  1004. diff = p_cli->p_ctx->cmd_buff_len - p_cli->p_ctx->cmd_buff_pos;
  1005. if (diff == 0)
  1006. {
  1007. return;
  1008. }
  1009. memmove(&p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos],
  1010. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos + 1],
  1011. diff);
  1012. --p_cli->p_ctx->cmd_buff_len;
  1013. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  1014. bool last_line = p_cons->cur_y == p_cons->cur_y_end ? true : false;
  1015. /* If last line of command is edited, there is no need for saving cursor position. */
  1016. if (last_line)
  1017. {
  1018. nrf_cli_fprintf(p_cli,
  1019. NRF_CLI_NORMAL,
  1020. "%s",
  1021. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos]);
  1022. NRF_CLI_VT100_CMD(p_cli, NRF_CLI_VT100_CLEAREOL);
  1023. cursor_left_move(p_cli, --diff);
  1024. }
  1025. else
  1026. {
  1027. cli_cursor_save(p_cli);
  1028. cli_clear_eos(p_cli);
  1029. nrf_cli_fprintf(p_cli,
  1030. NRF_CLI_NORMAL,
  1031. "%s",
  1032. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos]);
  1033. cli_cursor_restore(p_cli);
  1034. }
  1035. }
  1036. static char make_argv(size_t * p_argc, char ** pp_argv, char * p_cmd, uint8_t max_argc)
  1037. {
  1038. char c;
  1039. char quote = 0;
  1040. *p_argc = 0;
  1041. do
  1042. {
  1043. c = *p_cmd;
  1044. if (c == '\0')
  1045. {
  1046. break;
  1047. }
  1048. if (isspace((int)c))
  1049. {
  1050. *p_cmd++ = '\0';
  1051. continue;
  1052. }
  1053. pp_argv[(*p_argc)++] = p_cmd;
  1054. quote = 0;
  1055. while (1)
  1056. {
  1057. c = *p_cmd;
  1058. if (c == '\0')
  1059. {
  1060. break;
  1061. }
  1062. if (!quote)
  1063. {
  1064. switch (c)
  1065. {
  1066. case '\\':
  1067. memmove(p_cmd, p_cmd + 1, cli_strlen(p_cmd));
  1068. p_cmd += 1;
  1069. continue;
  1070. case '\'':
  1071. case '\"':
  1072. memmove(p_cmd, p_cmd + 1, cli_strlen(p_cmd));
  1073. quote = c;
  1074. continue;
  1075. default:
  1076. break;
  1077. }
  1078. }
  1079. if (quote == c)
  1080. {
  1081. memmove(p_cmd, p_cmd + 1, cli_strlen(p_cmd));
  1082. quote = 0;
  1083. continue;
  1084. }
  1085. if (quote && c == '\\')
  1086. {
  1087. char t = *(p_cmd + 1);
  1088. if (t == quote)
  1089. {
  1090. memmove(p_cmd, p_cmd + 1, cli_strlen(p_cmd));
  1091. p_cmd += 1;
  1092. continue;
  1093. }
  1094. if (t == '0')
  1095. {
  1096. uint8_t i;
  1097. uint8_t v = 0;
  1098. for (i = 2; i < (2 + 3); i++)
  1099. {
  1100. t = *(p_cmd + i);
  1101. if (t >= '0' && t <= '7')
  1102. {
  1103. v = (v << 3) | (t - '0');
  1104. }
  1105. else
  1106. {
  1107. break;
  1108. }
  1109. }
  1110. if (i > 2)
  1111. {
  1112. memmove(p_cmd, p_cmd + (i - 1), cli_strlen(p_cmd) - (i - 2));
  1113. *p_cmd++ = v;
  1114. continue;
  1115. }
  1116. }
  1117. if (t == 'x')
  1118. {
  1119. uint8_t i;
  1120. uint8_t v = 0;
  1121. for (i = 2; i < (2 + 2); i++)
  1122. {
  1123. t = *(p_cmd + i);
  1124. if (t >= '0' && t <= '9')
  1125. {
  1126. v = (v << 4) | (t - '0');
  1127. }
  1128. else if (t >= 'a' && t <= 'f')
  1129. {
  1130. v = (v << 4) | (t - 'a' + 10);
  1131. }
  1132. else if (t >= 'A' && t <= 'F')
  1133. {
  1134. v = (v << 4) | (t - 'A' + 10);
  1135. }
  1136. else
  1137. {
  1138. break;
  1139. }
  1140. }
  1141. if (i > 2)
  1142. {
  1143. memmove(p_cmd, p_cmd + (i - 1), cli_strlen(p_cmd) - (i - 2));
  1144. *p_cmd++ = v;
  1145. continue;
  1146. }
  1147. }
  1148. }
  1149. if (!quote && isspace((int)c))
  1150. {
  1151. break;
  1152. }
  1153. p_cmd += 1;
  1154. }
  1155. } while (*p_argc < max_argc);
  1156. ASSERT(*p_argc <= NRF_CLI_ARGC_MAX);
  1157. pp_argv[*p_argc] = 0;
  1158. return quote;
  1159. }
  1160. static void cli_state_set(nrf_cli_t const * p_cli, nrf_cli_state_t state)
  1161. {
  1162. p_cli->p_ctx->state = state;
  1163. if (state == NRF_CLI_STATE_ACTIVE)
  1164. {
  1165. cli_cmd_buffer_clear(p_cli);
  1166. nrf_cli_fprintf(p_cli, NRF_CLI_INFO, "%s", p_cli->p_name);
  1167. }
  1168. }
  1169. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  1170. static inline void history_mode_exit(nrf_cli_t const * p_cli)
  1171. {
  1172. p_cli->p_ctx->p_cmd_list_element = NULL;
  1173. }
  1174. static void history_handle(nrf_cli_t const * p_cli, bool up)
  1175. {
  1176. nrf_cli_memobj_header_t header = {
  1177. .p_prev = NULL,
  1178. .p_next = NULL,
  1179. .cmd_len = 0
  1180. };
  1181. nrf_cli_cmd_len_t current_cmd_len;
  1182. bool skip = false;
  1183. if (!up) /* button down */
  1184. {
  1185. if (p_cli->p_ctx->p_cmd_list_element == NULL)
  1186. {
  1187. return;
  1188. }
  1189. cursor_home_position_move(p_cli);
  1190. nrf_memobj_read(p_cli->p_ctx->p_cmd_list_element,
  1191. &header,
  1192. NRF_CLI_HISTORY_HEADER_SIZE,
  1193. 0);
  1194. p_cli->p_ctx->p_cmd_list_element = header.p_next;
  1195. current_cmd_len = p_cli->p_ctx->cmd_buff_len;
  1196. if (p_cli->p_ctx->p_cmd_list_element == NULL)
  1197. {
  1198. if (cli_strlen(p_cli->p_ctx->temp_buff) > 0)
  1199. {
  1200. strcpy(p_cli->p_ctx->cmd_buff, p_cli->p_ctx->temp_buff);
  1201. }
  1202. else
  1203. {
  1204. p_cli->p_ctx->cmd_buff[0] = '\0';
  1205. }
  1206. header.cmd_len = cli_strlen(p_cli->p_ctx->cmd_buff);
  1207. skip = true;
  1208. }
  1209. }
  1210. else /* button up */
  1211. {
  1212. if ((p_cli->p_ctx->p_cmd_list_element == p_cli->p_ctx->p_cmd_list_tail) ||
  1213. (p_cli->p_ctx->p_cmd_list_head == NULL))
  1214. {
  1215. /* Nothing to display. */
  1216. return;
  1217. }
  1218. cursor_home_position_move(p_cli);
  1219. if (p_cli->p_ctx->p_cmd_list_element == NULL)
  1220. {
  1221. current_cmd_len = cli_strlen(p_cli->p_ctx->cmd_buff);
  1222. p_cli->p_ctx->p_cmd_list_element = p_cli->p_ctx->p_cmd_list_head;
  1223. /* Save the currently entered and not executed command. */
  1224. if (current_cmd_len > 0)
  1225. {
  1226. strcpy(p_cli->p_ctx->temp_buff, p_cli->p_ctx->cmd_buff);
  1227. }
  1228. else
  1229. {
  1230. p_cli->p_ctx->temp_buff[0] = '\0';
  1231. }
  1232. }
  1233. else
  1234. {
  1235. nrf_memobj_read(p_cli->p_ctx->p_cmd_list_element,
  1236. &header,
  1237. NRF_CLI_HISTORY_HEADER_SIZE,
  1238. 0);
  1239. current_cmd_len = header.cmd_len;
  1240. p_cli->p_ctx->p_cmd_list_element = header.p_prev;
  1241. }
  1242. }
  1243. if (!skip)
  1244. {
  1245. nrf_memobj_read(p_cli->p_ctx->p_cmd_list_element,
  1246. &header,
  1247. NRF_CLI_HISTORY_HEADER_SIZE,
  1248. 0);
  1249. nrf_memobj_read(p_cli->p_ctx->p_cmd_list_element,
  1250. p_cli->p_ctx->cmd_buff,
  1251. header.cmd_len + 1, /* +1 for '\0' */
  1252. NRF_CLI_HISTORY_HEADER_SIZE);
  1253. }
  1254. p_cli->p_ctx->cmd_buff_pos = header.cmd_len;
  1255. p_cli->p_ctx->cmd_buff_len = header.cmd_len;
  1256. if (current_cmd_len > header.cmd_len)
  1257. {
  1258. cli_clear_eos(p_cli);
  1259. }
  1260. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "%s", p_cli->p_ctx->cmd_buff);
  1261. if (cursor_in_empty_line(p_cli) || full_line_cmd(p_cli))
  1262. {
  1263. cursor_next_line_move(p_cli);
  1264. }
  1265. }
  1266. static void history_list_element_add(nrf_cli_t const * p_cli, nrf_memobj_t * p_memobj)
  1267. {
  1268. ASSERT(p_memobj != NULL);
  1269. nrf_cli_memobj_header_t header;
  1270. if (p_cli->p_ctx->p_cmd_list_head == NULL)
  1271. {
  1272. p_cli->p_ctx->p_cmd_list_head = p_memobj;
  1273. p_cli->p_ctx->p_cmd_list_tail = p_memobj;
  1274. header.p_prev = NULL;
  1275. header.p_next = NULL;
  1276. header.cmd_len = p_cli->p_ctx->cmd_buff_len;
  1277. }
  1278. else
  1279. {
  1280. nrf_memobj_read(p_cli->p_ctx->p_cmd_list_head,
  1281. &header,
  1282. NRF_CLI_HISTORY_HEADER_SIZE,
  1283. 0);
  1284. header.p_next = p_memobj;
  1285. nrf_memobj_write(p_cli->p_ctx->p_cmd_list_head,
  1286. &header,
  1287. NRF_CLI_HISTORY_HEADER_SIZE,
  1288. 0);
  1289. header.p_next = NULL;
  1290. header.p_prev = p_cli->p_ctx->p_cmd_list_head;
  1291. header.cmd_len = p_cli->p_ctx->cmd_buff_len;
  1292. p_cli->p_ctx->p_cmd_list_head = p_memobj;
  1293. }
  1294. nrf_memobj_write(p_memobj,
  1295. &header,
  1296. NRF_CLI_HISTORY_HEADER_SIZE,
  1297. 0);
  1298. nrf_memobj_write(p_memobj,
  1299. p_cli->p_ctx->cmd_buff,
  1300. p_cli->p_ctx->cmd_buff_len + 1, /* +1 for '\0' */
  1301. NRF_CLI_HISTORY_HEADER_SIZE);
  1302. }
  1303. static void history_list_element_oldest_remove(nrf_cli_t const * p_cli)
  1304. {
  1305. if (p_cli->p_ctx->p_cmd_list_tail == NULL)
  1306. {
  1307. return; // nothing to do
  1308. }
  1309. nrf_cli_memobj_header_t header;
  1310. nrf_memobj_t * p_memobj = p_cli->p_ctx->p_cmd_list_tail;
  1311. nrf_memobj_read(p_memobj,
  1312. &header,
  1313. NRF_CLI_HISTORY_HEADER_SIZE,
  1314. 0);
  1315. p_cli->p_ctx->p_cmd_list_tail = header.p_next;
  1316. memset(&header, 0, sizeof(nrf_cli_memobj_header_t));
  1317. nrf_memobj_write(p_memobj, &header, NRF_CLI_HISTORY_HEADER_SIZE, 0);
  1318. nrf_memobj_free(p_memobj);
  1319. /* Checking if memory objects list is empty. */
  1320. if (p_cli->p_ctx->p_cmd_list_tail == NULL)
  1321. {
  1322. p_cli->p_ctx->p_cmd_list_head = NULL;
  1323. return;
  1324. }
  1325. nrf_memobj_read(p_cli->p_ctx->p_cmd_list_tail,
  1326. &header,
  1327. NRF_CLI_HISTORY_HEADER_SIZE,
  1328. 0);
  1329. header.p_prev = NULL;
  1330. nrf_memobj_write(p_cli->p_ctx->p_cmd_list_tail, &header, NRF_CLI_HISTORY_HEADER_SIZE, 0);
  1331. }
  1332. static void history_list_free_memory(nrf_cli_t const * p_cli)
  1333. {
  1334. while (p_cli->p_ctx->p_cmd_list_tail != NULL)
  1335. {
  1336. history_list_element_oldest_remove(p_cli);
  1337. }
  1338. }
  1339. static void history_save(nrf_cli_t const * p_cli)
  1340. {
  1341. nrf_cli_cmd_len_t cmd_new_len = cli_strlen(p_cli->p_ctx->cmd_buff);
  1342. history_mode_exit(p_cli);
  1343. if (cmd_new_len == 0)
  1344. {
  1345. return;
  1346. }
  1347. /* Checking if newly entered command is not duplicated with previous one. */
  1348. if (p_cli->p_ctx->p_cmd_list_head != NULL)
  1349. {
  1350. nrf_cli_memobj_header_t header;
  1351. nrf_memobj_read(p_cli->p_ctx->p_cmd_list_head,
  1352. &header,
  1353. NRF_CLI_HISTORY_HEADER_SIZE,
  1354. 0);
  1355. if (cmd_new_len == header.cmd_len)
  1356. {
  1357. nrf_memobj_read(p_cli->p_ctx->p_cmd_list_head,
  1358. p_cli->p_ctx->temp_buff,
  1359. header.cmd_len + 1, /* +1 for '\0' */
  1360. NRF_CLI_HISTORY_HEADER_SIZE);
  1361. if (strcmp(p_cli->p_ctx->cmd_buff, p_cli->p_ctx->temp_buff) == 0)
  1362. {
  1363. /* Duplicated command, nothing to save. */
  1364. p_cli->p_ctx->temp_buff[0] = '\0';
  1365. return;
  1366. }
  1367. p_cli->p_ctx->temp_buff[0] = '\0';
  1368. }
  1369. }
  1370. for (size_t idx = 0; idx < NRF_CLI_HISTORY_ELEMENT_COUNT; idx++)
  1371. {
  1372. nrf_memobj_t * p_memobj;
  1373. p_memobj = nrf_memobj_alloc(p_cli->p_cmd_hist_mempool,
  1374. ((size_t)NRF_CLI_HISTORY_HEADER_SIZE + cmd_new_len + 1));
  1375. if (p_memobj != NULL)
  1376. {
  1377. history_list_element_add(p_cli, p_memobj);
  1378. return;
  1379. }
  1380. else
  1381. {
  1382. history_list_element_oldest_remove(p_cli);
  1383. }
  1384. }
  1385. return;
  1386. }
  1387. #endif // NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  1388. /* Function checks how many identical characters have two strings starting from the first character. */
  1389. static nrf_cli_cmd_len_t str_similarity_check(char const * str_a, char const * str_b)
  1390. {
  1391. nrf_cli_cmd_len_t cnt = 0;
  1392. while (str_a[cnt] != '\0')
  1393. {
  1394. if (str_a[cnt] != str_b[cnt])
  1395. {
  1396. return cnt;
  1397. }
  1398. if (++cnt == 0)
  1399. {
  1400. return --cnt; /* too long strings */
  1401. }
  1402. }
  1403. return cnt;
  1404. }
  1405. static void completion_insert(nrf_cli_t const * p_cli,
  1406. char const * p_compl,
  1407. nrf_cli_cmd_len_t compl_len)
  1408. {
  1409. ASSERT (p_compl);
  1410. nrf_cli_cmd_len_t diff = p_cli->p_ctx->cmd_buff_len - p_cli->p_ctx->cmd_buff_pos;
  1411. if ((p_cli->p_ctx->cmd_buff_len + compl_len > NRF_CLI_CMD_BUFF_SIZE - 1) ||
  1412. (compl_len == 0))
  1413. {
  1414. return;
  1415. }
  1416. /* Make space for completion. */
  1417. memmove(&p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos + compl_len],
  1418. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos],
  1419. diff + 1); /* + 1 for '\0' */
  1420. /* Insert completion. */
  1421. memmove(&p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos],
  1422. p_compl,
  1423. compl_len);
  1424. p_cli->p_ctx->cmd_buff_len = cli_strlen(p_cli->p_ctx->cmd_buff);
  1425. nrf_cli_fprintf(p_cli,
  1426. NRF_CLI_NORMAL,
  1427. "%s",
  1428. &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos]);
  1429. p_cli->p_ctx->cmd_buff_pos += compl_len;
  1430. if (cursor_in_empty_line(p_cli) || full_line_cmd(p_cli))
  1431. {
  1432. cursor_next_line_move(p_cli);
  1433. }
  1434. if (diff > 0)
  1435. {
  1436. cursor_position_synchronize(p_cli);
  1437. }
  1438. }
  1439. static void option_print(nrf_cli_t const * p_cli,
  1440. char const * p_option,
  1441. nrf_cli_cmd_len_t longest_option)
  1442. {
  1443. static char const * tab = " ";
  1444. /* Function initialization has been requested. */
  1445. if (p_option == NULL)
  1446. {
  1447. p_cli->p_ctx->vt100_ctx.printed_cmd = 0;
  1448. return;
  1449. }
  1450. longest_option += cli_strlen(tab);
  1451. nrf_cli_cmd_len_t columns =
  1452. (p_cli->p_ctx->vt100_ctx.cons.terminal_wid - cli_strlen(tab)) / longest_option;
  1453. nrf_cli_cmd_len_t diff = longest_option - cli_strlen(p_option);
  1454. if (p_cli->p_ctx->vt100_ctx.printed_cmd++ % columns == 0)
  1455. {
  1456. nrf_cli_fprintf(p_cli, NRF_CLI_OPTION, "\n%s%s", tab, p_option);
  1457. }
  1458. else
  1459. {
  1460. nrf_cli_fprintf(p_cli, NRF_CLI_OPTION, "%s", p_option);
  1461. }
  1462. cursor_right_move(p_cli, diff);
  1463. }
  1464. static inline bool is_completion_candidate(const char *candidate,
  1465. const char *str,
  1466. size_t len)
  1467. {
  1468. return (strncmp(candidate, str, len) == 0) ? true : false;
  1469. }
  1470. static void cli_tab_handle(nrf_cli_t const * p_cli)
  1471. {
  1472. size_t cmd_idx;
  1473. size_t cmd_last = 0;
  1474. size_t cmd_first = 0;
  1475. size_t cmd_cnt = 0;
  1476. size_t argc;
  1477. char * argv[NRF_CLI_ARGC_MAX + 1]; /* +1 reserved for NULL in function make_argv */
  1478. nrf_cli_cmd_len_t cmd_lvl = NRF_CLI_CMD_ROOT_LVL;
  1479. nrf_cli_cmd_len_t cmd_longest = 0; /* longest matching command */
  1480. /* Calculating the longest possible completion length. -1 for '\0'. */
  1481. nrf_cli_cmd_len_t compl_len = (NRF_CLI_CMD_BUFF_SIZE - 1) - p_cli->p_ctx->cmd_buff_len;
  1482. if (compl_len == 0)
  1483. {
  1484. return;
  1485. }
  1486. /* Copy command from its beginning to cursor position. */
  1487. memcpy(p_cli->p_ctx->temp_buff,
  1488. p_cli->p_ctx->cmd_buff,
  1489. p_cli->p_ctx->cmd_buff_pos);
  1490. p_cli->p_ctx->temp_buff[p_cli->p_ctx->cmd_buff_pos] = '\0';
  1491. /* Check if the current cursor position points to the 'space' character. */
  1492. bool space = isspace((int)p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos - 1]);
  1493. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  1494. /* If the Tab key is pressed, "history mode" must be terminated because tab and history handlers
  1495. are sharing the same array: temp_buff. */
  1496. history_mode_exit(p_cli);
  1497. #endif
  1498. /* Create argument list. */
  1499. (void)make_argv(&argc,
  1500. &argv[0],
  1501. p_cli->p_ctx->temp_buff,
  1502. NRF_CLI_ARGC_MAX);
  1503. nrf_cli_cmd_len_t arg_len = cli_strlen(argv[cmd_lvl]);
  1504. /* Variable 'static_entry' is needed to handle dynamic commands. */
  1505. nrf_cli_static_entry_t static_entry;
  1506. nrf_cli_cmd_entry_t const * p_cmd = NULL;
  1507. nrf_cli_static_entry_t const * p_st_cmd = NULL;
  1508. nrf_cli_static_entry_t const * p_st_cmd_last = NULL;
  1509. do
  1510. {
  1511. if ((argc == 0) ||
  1512. (cmd_lvl >= argc - 1 + space))
  1513. {
  1514. if (space)
  1515. {
  1516. arg_len = 0;
  1517. }
  1518. else
  1519. {
  1520. arg_len = cli_strlen(argv[cmd_lvl]);
  1521. }
  1522. cmd_idx = 0;
  1523. while (1)
  1524. {
  1525. cmd_get(p_cmd, cmd_lvl, cmd_idx++, &p_st_cmd, &static_entry);
  1526. if (p_st_cmd == NULL)
  1527. {
  1528. /* No more commands available. */
  1529. break;
  1530. }
  1531. if (!is_completion_candidate(argv[cmd_lvl],
  1532. p_st_cmd->p_syntax,
  1533. arg_len))
  1534. {
  1535. continue;
  1536. }
  1537. cmd_cnt++;
  1538. if (p_st_cmd_last == NULL)
  1539. {
  1540. cmd_first = cmd_idx - 1;
  1541. cmd_longest = cli_strlen(p_st_cmd->p_syntax);
  1542. if (compl_len > (cmd_longest - arg_len))
  1543. {
  1544. compl_len = cmd_longest - arg_len;
  1545. }
  1546. }
  1547. else
  1548. {
  1549. nrf_cli_cmd_len_t len = cli_strlen(p_st_cmd->p_syntax);
  1550. if (len > cmd_longest)
  1551. {
  1552. cmd_longest = len;
  1553. }
  1554. if (compl_len > 0) /* Checking if partial completion is possible */
  1555. {
  1556. nrf_cli_static_entry_t last_entry;
  1557. cmd_get(p_cmd, cmd_lvl, cmd_last, &p_st_cmd_last, &last_entry);
  1558. len = str_similarity_check(p_st_cmd->p_syntax + arg_len,
  1559. p_st_cmd_last->p_syntax + arg_len);
  1560. if (compl_len > len)
  1561. {
  1562. /* Determining the longest possible completion. */
  1563. compl_len = len;
  1564. }
  1565. }
  1566. }
  1567. cmd_last = cmd_idx - 1;
  1568. p_st_cmd_last = p_st_cmd;
  1569. if (cmd_idx == 0) /* Too many possibilities */
  1570. {
  1571. nrf_cli_warn(p_cli, NRF_CLI_MSG_TAB_OVERFLOWED);
  1572. break;
  1573. }
  1574. }
  1575. }
  1576. else
  1577. {
  1578. cmd_idx = 0;
  1579. while (1)
  1580. {
  1581. cmd_get(p_cmd, cmd_lvl, cmd_idx++, &p_st_cmd, &static_entry);
  1582. if (cmd_idx == 0)
  1583. {
  1584. /* No match found and commands counter overflowed. */
  1585. nrf_cli_warn(p_cli, NRF_CLI_MSG_TAB_OVERFLOWED);
  1586. return;
  1587. }
  1588. if (p_st_cmd == NULL) /* No more commands available */
  1589. {
  1590. return;
  1591. }
  1592. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  1593. /* Ignore wildcard character arguments if they are "standalone". Example:
  1594. 1. log enable info b*<tab> -> "b*" is treated as a command so no match found
  1595. 2. log enable info b* <tab> -> "b* " is ignored, <tab> will prompt all available
  1596. commands. */
  1597. if (wildcard_character_exist(argv[cmd_lvl]))
  1598. {
  1599. break;
  1600. }
  1601. #endif
  1602. /* Fuction "strcmp" is used because an exact match is required. */
  1603. if (strcmp(argv[cmd_lvl], p_st_cmd->p_syntax) == 0)
  1604. {
  1605. p_cmd = p_st_cmd->p_subcmd;
  1606. break;
  1607. }
  1608. }
  1609. }
  1610. if ((p_cmd == NULL) || (p_st_cmd == NULL))
  1611. {
  1612. break;
  1613. }
  1614. } while (++cmd_lvl < argc + space);
  1615. if (cmd_cnt == 0)
  1616. {
  1617. /* No match found. */
  1618. return;
  1619. }
  1620. if (cmd_cnt == 1) /* only one match found */
  1621. {
  1622. if (p_cmd->is_dynamic)
  1623. {
  1624. /* In case of dynamic entry, function cmd_get shall be called again for matching
  1625. * command index (cmd_last). It is because static_entry is most likely appended by
  1626. * not valid data.
  1627. */
  1628. cmd_get(p_cmd, cmd_lvl, cmd_last, &p_st_cmd_last, &static_entry);
  1629. }
  1630. if (cli_strlen(p_st_cmd_last->p_syntax) != arg_len) /* no exact match found */
  1631. {
  1632. completion_insert(p_cli, p_st_cmd_last->p_syntax + arg_len, compl_len);
  1633. }
  1634. /* Next character in the buffer is not 'space'. */
  1635. if (!isspace((int)p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_pos]))
  1636. {
  1637. if (p_cli->p_ctx->internal.flag.insert_mode)
  1638. {
  1639. p_cli->p_ctx->internal.flag.insert_mode = 0;
  1640. char_insert(p_cli, ' ');
  1641. p_cli->p_ctx->internal.flag.insert_mode = 1;
  1642. }
  1643. else
  1644. {
  1645. char_insert(p_cli, ' ');
  1646. }
  1647. }
  1648. else
  1649. {
  1650. /* case:
  1651. | | -> cursor
  1652. cons_name $: valid_cmd valid_sub_cmd| |argument <tab>
  1653. */
  1654. cursor_position_increment(p_cli);
  1655. /* result:
  1656. cons_name $: valid_cmd valid_sub_cmd |a|rgument
  1657. */
  1658. }
  1659. return;
  1660. }
  1661. /* Printing all matching commands (options). */
  1662. option_print(p_cli, NRF_CLI_INIT_OPTION_PRINTER, cmd_longest);
  1663. cmd_idx = cmd_first;
  1664. while (cmd_cnt)
  1665. {
  1666. cmd_get(p_cmd, cmd_lvl, cmd_idx++, &p_st_cmd, &static_entry);
  1667. if (!is_completion_candidate(argv[cmd_lvl],
  1668. p_st_cmd->p_syntax,
  1669. arg_len))
  1670. {
  1671. continue;
  1672. }
  1673. cmd_cnt--;
  1674. option_print(p_cli, p_st_cmd->p_syntax, cmd_longest);
  1675. }
  1676. nrf_cli_fprintf(p_cli, NRF_CLI_INFO, "\n%s", p_cli->p_name);
  1677. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "%s", p_cli->p_ctx->cmd_buff);
  1678. cursor_position_synchronize(p_cli);
  1679. completion_insert(p_cli, p_st_cmd_last->p_syntax + arg_len, compl_len);
  1680. }
  1681. /* Functions returns true if new line character shall be processed */
  1682. static bool process_nl(nrf_cli_t const * p_cli, uint8_t data)
  1683. {
  1684. if ((data != '\r') && (data != '\n'))
  1685. {
  1686. cli_flag_last_nl_set(p_cli, 0);
  1687. return false;
  1688. }
  1689. if ((cli_flag_last_nl_get(p_cli) == 0) ||
  1690. (data == cli_flag_last_nl_get(p_cli)))
  1691. {
  1692. cli_flag_last_nl_set(p_cli, data);
  1693. return true;
  1694. }
  1695. return false;
  1696. }
  1697. #define NRF_CLI_ASCII_MAX_CHAR (127u)
  1698. static inline ret_code_t ascii_filter(char const data)
  1699. {
  1700. return (uint8_t)data > NRF_CLI_ASCII_MAX_CHAR ? NRF_ERROR_INVALID_DATA : NRF_SUCCESS;
  1701. }
  1702. static void cli_state_collect(nrf_cli_t const * p_cli)
  1703. {
  1704. size_t count = 0;
  1705. char data;
  1706. while (1)
  1707. {
  1708. cli_read(p_cli, &data, sizeof(data), &count);
  1709. if (count == 0)
  1710. {
  1711. return;
  1712. }
  1713. if (ascii_filter(data) != NRF_SUCCESS)
  1714. {
  1715. continue;
  1716. }
  1717. #if NRF_MODULE_ENABLED(NRF_PWR_MGMT)
  1718. nrf_pwr_mgmt_feed();
  1719. #endif
  1720. switch (p_cli->p_ctx->receive_state)
  1721. {
  1722. case NRF_CLI_RECEIVE_DEFAULT:
  1723. if (process_nl(p_cli, data))
  1724. {
  1725. if (p_cli->p_ctx->cmd_buff_len == 0)
  1726. {
  1727. history_mode_exit(p_cli);
  1728. cursor_next_line_move(p_cli);
  1729. }
  1730. else
  1731. {
  1732. /* Command execution */
  1733. cli_execute(p_cli);
  1734. }
  1735. cli_state_set(p_cli, NRF_CLI_STATE_ACTIVE);
  1736. return;
  1737. }
  1738. switch (data)
  1739. {
  1740. case NRF_CLI_VT100_ASCII_ESC: /* ESCAPE */
  1741. receive_state_change(p_cli, NRF_CLI_RECEIVE_ESC);
  1742. break;
  1743. case '\0':
  1744. break;
  1745. #if NRF_MODULE_ENABLED(NRF_CLI_METAKEYS)
  1746. case NRF_CLI_VT100_ASCII_CTRL_A: /* CTRL + A */
  1747. cursor_home_position_move(p_cli);
  1748. break;
  1749. case NRF_CLI_VT100_ASCII_CTRL_C: /* CTRL + C */
  1750. cursor_end_position_move(p_cli);
  1751. if (!cursor_in_empty_line(p_cli))
  1752. {
  1753. cursor_next_line_move(p_cli);
  1754. }
  1755. cli_state_set(p_cli, NRF_CLI_STATE_ACTIVE);
  1756. break;
  1757. case NRF_CLI_VT100_ASCII_CTRL_E: /* CTRL + E */
  1758. cursor_end_position_move(p_cli);
  1759. break;
  1760. case NRF_CLI_VT100_ASCII_CTRL_L: /* CTRL + L */
  1761. NRF_CLI_VT100_CMD(p_cli, NRF_CLI_VT100_CURSORHOME);
  1762. NRF_CLI_VT100_CMD(p_cli, NRF_CLI_VT100_CLEARSCREEN);
  1763. nrf_cli_fprintf(p_cli, NRF_CLI_INFO, "%s", p_cli->p_name);
  1764. if (cli_flag_echo_is_set(p_cli))
  1765. {
  1766. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "%s", p_cli->p_ctx->cmd_buff);
  1767. cursor_position_synchronize(p_cli);
  1768. }
  1769. break;
  1770. case NRF_CLI_VT100_ASCII_CTRL_U: /* CTRL + U */
  1771. cursor_home_position_move(p_cli);
  1772. cli_cmd_buffer_clear(p_cli);
  1773. cli_clear_eos(p_cli);
  1774. break;
  1775. case NRF_CLI_VT100_ASCII_CTRL_W: /* CTRL + W */
  1776. cli_cmd_word_remove(p_cli);
  1777. break;
  1778. #endif
  1779. case '\t': /* TAB */
  1780. if (cli_flag_echo_is_set(p_cli))
  1781. {
  1782. cli_tab_handle(p_cli);
  1783. }
  1784. break;
  1785. case NRF_CLI_VT100_ASCII_BSPACE: /* BACKSPACE */
  1786. if (cli_flag_echo_is_set(p_cli))
  1787. {
  1788. char_backspace(p_cli);
  1789. }
  1790. break;
  1791. case NRF_CLI_VT100_ASCII_DEL: /* DELETE */
  1792. if (cli_flag_echo_is_set(p_cli))
  1793. {
  1794. char_delete(p_cli);
  1795. }
  1796. break;
  1797. default:
  1798. if (isprint((int)data))
  1799. {
  1800. if (cli_flag_echo_is_set(p_cli))
  1801. {
  1802. char_insert(p_cli, data);
  1803. }
  1804. else
  1805. {
  1806. char_insert_echo_off(p_cli, data);
  1807. }
  1808. }
  1809. break;
  1810. }
  1811. break;
  1812. case NRF_CLI_RECEIVE_ESC:
  1813. if (data == '[')
  1814. {
  1815. receive_state_change(p_cli, NRF_CLI_RECEIVE_ESC_SEQ);
  1816. }
  1817. else
  1818. {
  1819. receive_state_change(p_cli, NRF_CLI_RECEIVE_DEFAULT);
  1820. }
  1821. break;
  1822. case NRF_CLI_RECEIVE_ESC_SEQ:
  1823. receive_state_change(p_cli, NRF_CLI_RECEIVE_DEFAULT);
  1824. if (!cli_flag_echo_is_set(p_cli))
  1825. {
  1826. return;
  1827. }
  1828. switch (data)
  1829. {
  1830. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  1831. case 'A': /* UP arrow */
  1832. history_handle(p_cli, true);
  1833. break;
  1834. case 'B': /* DOWN arrow */
  1835. history_handle(p_cli, false);
  1836. break;
  1837. #endif
  1838. case 'C': /* RIGHT arrow */
  1839. right_arrow_handle(p_cli);
  1840. break;
  1841. case 'D': /* LEFT arrow */
  1842. left_arrow_handle(p_cli);
  1843. break;
  1844. case '4': /* END Button in ESC[n~ mode */
  1845. receive_state_change(p_cli, NRF_CLI_RECEIVE_TILDE_EXP);
  1846. /* fall through */
  1847. case 'F': /* END Button in VT100 mode */
  1848. cursor_end_position_move(p_cli);
  1849. break;
  1850. case '1': /* HOME Button in ESC[n~ mode */
  1851. receive_state_change(p_cli, NRF_CLI_RECEIVE_TILDE_EXP);
  1852. /* fall through */
  1853. case 'H': /* HOME Button in VT100 mode */
  1854. cursor_home_position_move(p_cli);
  1855. break;
  1856. case '2': /* INSERT Button in ESC[n~ mode */
  1857. receive_state_change(p_cli, NRF_CLI_RECEIVE_TILDE_EXP);
  1858. /* fall through */
  1859. case 'L': /* INSERT Button in VT100 mode */
  1860. p_cli->p_ctx->internal.flag.insert_mode ^= 1;
  1861. break;
  1862. case '3':/* DELETE Button in ESC[n~ mode */
  1863. receive_state_change(p_cli, NRF_CLI_RECEIVE_TILDE_EXP);
  1864. if (cli_flag_echo_is_set(p_cli))
  1865. {
  1866. char_delete(p_cli);
  1867. }
  1868. break;
  1869. default:
  1870. break;
  1871. }
  1872. break;
  1873. case NRF_CLI_RECEIVE_TILDE_EXP:
  1874. receive_state_change(p_cli, NRF_CLI_RECEIVE_DEFAULT);
  1875. break;
  1876. default:
  1877. receive_state_change(p_cli, NRF_CLI_RECEIVE_DEFAULT);
  1878. break;
  1879. }
  1880. }
  1881. }
  1882. /* Function remove white chars from beginning and end of command buffer. */
  1883. static void cmd_trim(nrf_cli_t const * p_cli)
  1884. {
  1885. nrf_cli_cmd_len_t i = 0;
  1886. if (p_cli->p_ctx->cmd_buff[0] == '\0') /* no command in the buffer */
  1887. {
  1888. return;
  1889. }
  1890. /* Counting white characters starting from beginning of the command. */
  1891. while (isspace((int)p_cli->p_ctx->cmd_buff[i++]))
  1892. {
  1893. if (i == 0)
  1894. {
  1895. p_cli->p_ctx->cmd_buff[0] = '\0';
  1896. return;
  1897. }
  1898. }
  1899. /* Removing counted white characters. */
  1900. if (--i > 0)
  1901. {
  1902. memmove(p_cli->p_ctx->cmd_buff,
  1903. p_cli->p_ctx->cmd_buff + i,
  1904. (p_cli->p_ctx->cmd_buff_len + 1) - i); /* +1 for '\0' */
  1905. p_cli->p_ctx->cmd_buff_len = p_cli->p_ctx->cmd_buff_len - i;
  1906. p_cli->p_ctx->cmd_buff_pos = p_cli->p_ctx->cmd_buff_len;
  1907. }
  1908. /* Counting white characters starting from end of command. */
  1909. char * p_end = &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_len - 1];
  1910. i = 0;
  1911. while (isspace((int)*p_end))
  1912. {
  1913. ++i;
  1914. --p_end;
  1915. }
  1916. /* Removing counted white characters. */
  1917. if (p_end != &p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_len - 1])
  1918. {
  1919. p_cli->p_ctx->cmd_buff[p_cli->p_ctx->cmd_buff_len - i] = '\0';
  1920. p_cli->p_ctx->cmd_buff_len = p_cli->p_ctx->cmd_buff_len - i;
  1921. p_cli->p_ctx->cmd_buff_pos = p_cli->p_ctx->cmd_buff_len;
  1922. }
  1923. }
  1924. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  1925. static void spaces_trim(char * p_char)
  1926. {
  1927. nrf_cli_cmd_len_t shift = 0;
  1928. nrf_cli_cmd_len_t len = cli_strlen(p_char);
  1929. if (p_char == NULL)
  1930. {
  1931. return;
  1932. }
  1933. for (nrf_cli_cmd_len_t i = 0; i < len - 1; i++)
  1934. {
  1935. if (isspace((int)p_char[i]))
  1936. {
  1937. for (nrf_cli_cmd_len_t j = i + 1; j < len; j++)
  1938. {
  1939. if (isspace((int)p_char[j]))
  1940. {
  1941. shift++;
  1942. continue;
  1943. }
  1944. if (shift > 0)
  1945. {
  1946. memmove(&p_char[i + 1], &p_char[j], len - shift + 1); // +1 for EOS
  1947. len -= shift;
  1948. shift = 0;
  1949. }
  1950. break;
  1951. }
  1952. }
  1953. }
  1954. }
  1955. /* Adds new command and one space just before pattern */
  1956. static bool command_to_tmp_buffer_add(nrf_cli_t const * p_cli,
  1957. char const * p_new_cmd,
  1958. char const * p_pattern)
  1959. {
  1960. nrf_cli_cmd_len_t cmd_len = cli_strlen(p_new_cmd);
  1961. nrf_cli_cmd_len_t shift;
  1962. char * p_cmd_source_addr;
  1963. /* +1 for space */
  1964. if (((size_t)p_cli->p_ctx->cmd_tmp_buff_len + cmd_len + 1) > NRF_CLI_CMD_BUFF_SIZE)
  1965. {
  1966. nrf_cli_warn(p_cli,
  1967. "Command buffer is too short to expand all commands matching "
  1968. "wildcard pattern");
  1969. return false;
  1970. }
  1971. p_cmd_source_addr = strstr(p_cli->p_ctx->temp_buff, p_pattern);
  1972. if (p_cmd_source_addr == NULL)
  1973. {
  1974. return false;
  1975. }
  1976. shift = cli_strlen(p_cmd_source_addr);
  1977. /* make place for new command: + 1 for space + 1 for EOS */
  1978. memmove(p_cmd_source_addr + cmd_len + 1, p_cmd_source_addr, shift + 1);
  1979. memcpy(p_cmd_source_addr, p_new_cmd, cmd_len);
  1980. p_cmd_source_addr[cmd_len] = ' ';
  1981. p_cli->p_ctx->cmd_tmp_buff_len += cmd_len + 1; // + 1 for space
  1982. return true;
  1983. }
  1984. /* removes pattern and following space */
  1985. static void pattern_from_tmp_buffer_remove(nrf_cli_t const * p_cli,
  1986. char const * p_pattern)
  1987. {
  1988. size_t shift;
  1989. char * p_pattern_addr = strstr(p_cli->p_ctx->temp_buff, p_pattern);
  1990. nrf_cli_cmd_len_t pattern_len = cli_strlen(p_pattern);
  1991. if (p_pattern_addr == NULL)
  1992. {
  1993. return;
  1994. }
  1995. if (p_pattern_addr > p_cli->p_ctx->temp_buff)
  1996. {
  1997. if (*(p_pattern_addr - 1) == ' ')
  1998. {
  1999. pattern_len++; /* space needs to be removed as well */
  2000. p_pattern_addr--; /* set pointer to space */
  2001. }
  2002. }
  2003. shift = cli_strlen(p_pattern_addr) - pattern_len + 1; /* +1 for EOS */
  2004. p_cli->p_ctx->cmd_tmp_buff_len -= pattern_len;
  2005. memmove(p_pattern_addr, p_pattern_addr + pattern_len, shift);
  2006. }
  2007. /**
  2008. * @internal @brief Function for searching and adding commands matching to wildcard pattern.
  2009. *
  2010. * This function is internal to nrf_cli module and shall be not called directly.
  2011. *
  2012. * @param[in/out] p_cli Pointer to the CLI instance.
  2013. * @param[in] p_cmd Pointer to command which will be processed
  2014. * @param[in] cmd_lvl Command level in the command tree.
  2015. * @param[in] p_pattern Pointer to wildcard pattern.
  2016. * @param[out] p_counter Number of found and added commands.
  2017. *
  2018. * @retval WILDCARD_CMD_ADDED All matching commands added to the buffer.
  2019. * @retval WILDCARD_CMD_ADDED_MISSING_SPACE Not all matching commands added because
  2020. * NRF_CLI_CMD_BUFF_SIZE is too small.
  2021. * @retval WILDCARD_CMD_NO_MATCH_FOUND No matching command found.
  2022. */
  2023. static wildcard_cmd_status_t commands_expand(nrf_cli_t const * p_cli,
  2024. nrf_cli_cmd_entry_t const * p_cmd,
  2025. size_t cmd_lvl,
  2026. char * p_pattern,
  2027. size_t * p_counter)
  2028. {
  2029. size_t cmd_idx = 0;
  2030. size_t counter = 0;
  2031. bool success = false;
  2032. nrf_cli_static_entry_t static_entry;
  2033. nrf_cli_static_entry_t const * p_static_entry = NULL;
  2034. wildcard_cmd_status_t ret_val = WILDCARD_CMD_NO_MATCH_FOUND;
  2035. do
  2036. {
  2037. cmd_get(p_cmd,
  2038. cmd_lvl,
  2039. cmd_idx++,
  2040. &p_static_entry,
  2041. &static_entry);
  2042. if (p_static_entry == NULL)
  2043. {
  2044. break;
  2045. }
  2046. if (0 == fnmatch(p_pattern, p_static_entry->p_syntax, 0))
  2047. {
  2048. success = command_to_tmp_buffer_add(p_cli,
  2049. p_static_entry->p_syntax,
  2050. p_pattern);
  2051. if (!success)
  2052. {
  2053. break;
  2054. }
  2055. counter++;
  2056. }
  2057. } while(cmd_idx != 0);
  2058. if (counter > 0)
  2059. {
  2060. *p_counter = counter;
  2061. pattern_from_tmp_buffer_remove(p_cli, p_pattern);
  2062. if (success)
  2063. {
  2064. ret_val = WILDCARD_CMD_ADDED;
  2065. }
  2066. else
  2067. {
  2068. ret_val = WILDCARD_CMD_ADDED_MISSING_SPACE;
  2069. }
  2070. }
  2071. return ret_val;
  2072. }
  2073. #endif // NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  2074. /* Function is analyzing the command buffer to find matching commands. Next, it invokes the last recognized
  2075. * command which has a handler and passes the rest of command buffer as arguments. */
  2076. static void cli_execute(nrf_cli_t const * p_cli)
  2077. {
  2078. char quote;
  2079. size_t argc;
  2080. char * argv[NRF_CLI_ARGC_MAX + 1]; /* +1 reserved for NULL added by function make_argv */
  2081. size_t cmd_idx; /* currently analyzed command in cmd_level */
  2082. size_t cmd_lvl = NRF_CLI_CMD_ROOT_LVL; /* currently analyzed command level */
  2083. size_t cmd_handler_lvl = 0; /* last command level for which a handler has been found */
  2084. nrf_cli_cmd_entry_t const * p_cmd = NULL;
  2085. cmd_trim(p_cli);
  2086. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  2087. history_save(p_cli);
  2088. #endif
  2089. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  2090. /* Wildcard can be correctly handled under following conditions:
  2091. - wildcard command does not have a handler
  2092. - wildcard command is on the deepest commands level
  2093. - other commands on the same level as wildcard command shall also not have a handler
  2094. Algorithm:
  2095. 1. Command buffer is copied to Temp buffer.
  2096. 2. Algorithm goes through Command buffer to find handlers and subcommands.
  2097. 3. If algorithm will find a wildcard character it switches to Temp buffer.
  2098. 4. In the Temp buffer command with found wildcard character is changed into matching command(s).
  2099. 5. Algorithm switch back to Command buffer and analyzes next command.
  2100. 6. When all arguments are analyzed from Command buffer, Temp buffer is copied to Command buffer.
  2101. 7. Last found handler is executed with all arguments in the Command buffer.
  2102. */
  2103. size_t commands_expanded = 0;
  2104. memset(p_cli->p_ctx->temp_buff, 0, sizeof(p_cli->p_ctx->temp_buff));
  2105. memcpy(p_cli->p_ctx->temp_buff,
  2106. p_cli->p_ctx->cmd_buff,
  2107. p_cli->p_ctx->cmd_buff_len);
  2108. /* Function spaces_trim must be used instead of make_argv. At this point it is important to keep
  2109. temp_buff as a one string. It will allow to find wildcard commands easly with strstr
  2110. function. */
  2111. spaces_trim(p_cli->p_ctx->temp_buff);
  2112. p_cli->p_ctx->cmd_tmp_buff_len = cli_strlen(p_cli->p_ctx->temp_buff) + 1; // +1 for EOS
  2113. #endif
  2114. cursor_end_position_move(p_cli);
  2115. if (!cursor_in_empty_line(p_cli))
  2116. {
  2117. cursor_next_line_move(p_cli);
  2118. }
  2119. /* create argument list */
  2120. quote = make_argv(&argc,
  2121. &argv[0],
  2122. p_cli->p_ctx->cmd_buff,
  2123. NRF_CLI_ARGC_MAX);
  2124. if (!argc)
  2125. {
  2126. cursor_next_line_move(p_cli);
  2127. return;
  2128. }
  2129. if (quote != 0)
  2130. {
  2131. nrf_cli_error(p_cli, "not terminated: %c", quote);
  2132. return;
  2133. }
  2134. /* Searching for a matching root command. */
  2135. for (cmd_idx = 0; cmd_idx <= CLI_DATA_SECTION_ITEM_COUNT; ++cmd_idx)
  2136. {
  2137. if (cmd_idx >= CLI_DATA_SECTION_ITEM_COUNT)
  2138. {
  2139. nrf_cli_error(p_cli, "%s%s", argv[0], NRF_CLI_MSG_COMMAND_NOT_FOUND);
  2140. return;
  2141. }
  2142. p_cmd = CLI_DATA_SECTION_ITEM_GET(cmd_idx);
  2143. if (strcmp(argv[cmd_lvl], p_cmd->u.p_static->p_syntax) != 0)
  2144. {
  2145. continue;
  2146. }
  2147. break;
  2148. }
  2149. /* Root command shall be always static. */
  2150. ASSERT(p_cmd->is_dynamic == false);
  2151. /* Memory reserved for dynamic commands. */
  2152. nrf_cli_static_entry_t static_entry;
  2153. nrf_cli_static_entry_t const * p_static_entry = NULL;
  2154. memset(&p_cli->p_ctx->active_cmd, 0, sizeof(p_cli->p_ctx->active_cmd));
  2155. if (p_cmd->u.p_static->handler != NULL)
  2156. {
  2157. p_cli->p_ctx->active_cmd = *p_cmd->u.p_static;
  2158. }
  2159. p_cmd = p_cmd->u.p_static->p_subcmd;
  2160. cmd_lvl++;
  2161. cmd_idx = 0;
  2162. while (1)
  2163. {
  2164. if (cmd_lvl >= argc)
  2165. {
  2166. break;
  2167. }
  2168. if (!strcmp(argv[cmd_lvl], "-h") || !strcmp(argv[cmd_lvl], "--help"))
  2169. {
  2170. /* Command called with help option so it makes no sense to search deeper commands. */
  2171. cli_flag_help_set(p_cli);
  2172. break;
  2173. }
  2174. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  2175. /* Wildcard character is found */
  2176. if (wildcard_character_exist(argv[cmd_lvl]))
  2177. {
  2178. size_t counter = 0;
  2179. wildcard_cmd_status_t status;
  2180. /* Function will search commands tree for commands matching wildcard pattern stored in
  2181. argv[cmd_lvl]. If match is found wildcard pattern will be replaced by matching
  2182. commands in temp_buffer. If there is no space to add all matching commands function
  2183. will add as many as possible. Next it will continue to search for next wildcard
  2184. pattern and it will try to add matching commands. */
  2185. status = commands_expand(p_cli, p_cmd, cmd_lvl, argv[cmd_lvl], &counter);
  2186. if (WILDCARD_CMD_NO_MATCH_FOUND == status)
  2187. {
  2188. break;
  2189. }
  2190. commands_expanded += counter;
  2191. cmd_lvl++;
  2192. continue;
  2193. }
  2194. #endif
  2195. cmd_get(p_cmd,
  2196. cmd_lvl,
  2197. cmd_idx++,
  2198. &p_static_entry,
  2199. &static_entry);
  2200. if ((cmd_idx == 0) || (p_static_entry == NULL))
  2201. {
  2202. break;
  2203. }
  2204. if (strcmp(argv[cmd_lvl], p_static_entry->p_syntax) == 0)
  2205. {
  2206. /* checking if command has a handler */
  2207. if (p_static_entry->handler != NULL)
  2208. {
  2209. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  2210. if (commands_expanded > 0)
  2211. {
  2212. cursor_end_position_move(p_cli);
  2213. if (!cursor_in_empty_line(p_cli))
  2214. {
  2215. cursor_next_line_move(p_cli);
  2216. }
  2217. /* An error occured, fnmatch argument cannot be followed by argument
  2218. * with a handler to avoid multiple function calls. */
  2219. nrf_cli_error(p_cli, "Error: requested multiple function executions");
  2220. cli_flag_help_clear(p_cli);
  2221. return;
  2222. }
  2223. #endif
  2224. /* Storing p_st_cmd->handler is not feasible for dynamic commands. Data will be
  2225. * invalid with the next loop iteration. */
  2226. cmd_handler_lvl = cmd_lvl;
  2227. p_cli->p_ctx->active_cmd = *p_static_entry;
  2228. }
  2229. cmd_lvl++;
  2230. cmd_idx = 0;
  2231. p_cmd = p_static_entry->p_subcmd;
  2232. }
  2233. }
  2234. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  2235. if (commands_expanded > 0)
  2236. {
  2237. /* Copy temp_buff to cmd_buff */
  2238. memcpy(p_cli->p_ctx->cmd_buff,
  2239. p_cli->p_ctx->temp_buff,
  2240. p_cli->p_ctx->cmd_tmp_buff_len);
  2241. p_cli->p_ctx->cmd_buff_len = p_cli->p_ctx->cmd_tmp_buff_len;
  2242. /* calling make_arg function again because cmd_buffer has additional commads */
  2243. (void)make_argv(&argc,
  2244. &argv[0],
  2245. p_cli->p_ctx->cmd_buff,
  2246. NRF_CLI_ARGC_MAX);
  2247. }
  2248. #endif
  2249. if (p_cli->p_ctx->active_cmd.handler != NULL)
  2250. {
  2251. p_cli->p_ctx->active_cmd.handler(p_cli,
  2252. argc - cmd_handler_lvl,
  2253. &argv[cmd_handler_lvl]);
  2254. }
  2255. else
  2256. {
  2257. nrf_cli_error(p_cli, NRF_CLI_MSG_SPECIFY_SUBCOMMAND);
  2258. }
  2259. cli_flag_help_clear(p_cli);
  2260. }
  2261. /* Function required by qsort. */
  2262. static int string_cmp(void const * pp_a, void const * pp_b)
  2263. {
  2264. ASSERT(pp_a);
  2265. ASSERT(pp_b);
  2266. char const ** pp_str_a = (char const **)pp_a;
  2267. char const ** pp_str_b = (char const **)pp_b;
  2268. return strcmp(*pp_str_a, *pp_str_b);
  2269. }
  2270. static void cli_transport_evt_handler(nrf_cli_transport_evt_t evt_type, void * p_context)
  2271. {
  2272. nrf_cli_t * p_cli = (nrf_cli_t *)p_context;
  2273. ASSERT(p_cli);
  2274. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  2275. task_events_set(p_cli->p_ctx->task_id, evt_type == NRF_CLI_TRANSPORT_EVT_RX_RDY ?
  2276. NRF_CLI_TRANSPORT_RX_RDY_TASK_EVT : NRF_CLI_TRANSPORT_TX_RDY_TASK_EVT);
  2277. #else
  2278. if (evt_type == NRF_CLI_TRANSPORT_EVT_RX_RDY)
  2279. {
  2280. }
  2281. else
  2282. {
  2283. /* wr done evt */
  2284. p_cli->p_ctx->internal.flag.tx_rdy = 1;
  2285. }
  2286. #endif
  2287. }
  2288. static ret_code_t nrf_cli_instance_init(nrf_cli_t const * p_cli,
  2289. void const * p_config,
  2290. bool use_colors)
  2291. {
  2292. ASSERT(p_cli);
  2293. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  2294. #if defined(NRF_CLI_LOG_BACKEND) && NRF_CLI_LOG_BACKEND
  2295. ((nrf_cli_log_backend_t *)p_cli->p_log_backend->p_ctx)->p_cli = p_cli;
  2296. #endif
  2297. ret_code_t ret = p_cli->p_iface->p_api->init(p_cli->p_iface,
  2298. p_config,
  2299. cli_transport_evt_handler,
  2300. (void *)p_cli);
  2301. if (ret != NRF_SUCCESS)
  2302. {
  2303. return ret;
  2304. }
  2305. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  2306. ASSERT(p_cli->p_cmd_hist_mempool);
  2307. ret = nrf_memobj_pool_init(p_cli->p_cmd_hist_mempool);
  2308. if (ret != NRF_SUCCESS)
  2309. {
  2310. return ret;
  2311. }
  2312. p_cli->p_ctx->p_cmd_list_head = NULL;
  2313. p_cli->p_ctx->p_cmd_list_tail = NULL;
  2314. #endif
  2315. memset(p_cli->p_ctx, 0, sizeof(nrf_cli_ctx_t));
  2316. p_cli->p_ctx->internal.flag.tx_rdy = 1;
  2317. #if NRF_MODULE_ENABLED(NRF_CLI_VT100_COLORS)
  2318. p_cli->p_ctx->internal.flag.use_colors = use_colors;
  2319. #endif
  2320. p_cli->p_ctx->internal.flag.echo = NRF_CLI_ECHO_STATUS;
  2321. p_cli->p_ctx->state = NRF_CLI_STATE_INITIALIZED;
  2322. p_cli->p_ctx->vt100_ctx.cons.terminal_wid = NRF_CLI_DEFAULT_TERMINAL_WIDTH;
  2323. p_cli->p_ctx->vt100_ctx.cons.terminal_hei = NRF_CLI_DEFAULT_TERMINAL_HEIGHT;
  2324. const char * * pp_sorted_cmds = (const char * *)CLI_SORTED_CMD_PTRS_START_ADDR_GET;
  2325. for (size_t i = 0; i < CLI_DATA_SECTION_ITEM_COUNT; i++)
  2326. {
  2327. const nrf_cli_cmd_entry_t * cmd;
  2328. cmd = CLI_DATA_SECTION_ITEM_GET(i);
  2329. /* NULL syntax commands not allowed. */
  2330. ASSERT(cmd);
  2331. ASSERT(cmd->u.p_static->p_syntax);
  2332. pp_sorted_cmds[i] = cmd->u.p_static->p_syntax;
  2333. }
  2334. if (CLI_DATA_SECTION_ITEM_COUNT > 0)
  2335. {
  2336. qsort(pp_sorted_cmds,
  2337. CLI_DATA_SECTION_ITEM_COUNT,
  2338. sizeof (char *),
  2339. string_cmp);
  2340. }
  2341. return NRF_SUCCESS;
  2342. }
  2343. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  2344. static ret_code_t nrf_cli_instance_uninit(nrf_cli_t const * p_cli);
  2345. void console_task(void * p_context)
  2346. {
  2347. nrf_cli_t * p_cli = (nrf_cli_t *)p_context;
  2348. ret_code_t ret = nrf_cli_start(p_cli);
  2349. APP_ERROR_CHECK(ret);
  2350. while (1)
  2351. {
  2352. uint32_t evts = task_events_wait(NRF_CLI_TASK_EVTS);
  2353. if (evts & NRF_CLI_KILL_TASK_EVT)
  2354. {
  2355. (void)nrf_cli_instance_uninit(p_cli);
  2356. task_exit();
  2357. }
  2358. else
  2359. {
  2360. nrf_cli_process(p_cli);
  2361. }
  2362. }
  2363. }
  2364. #endif
  2365. ret_code_t nrf_cli_init(nrf_cli_t const * p_cli,
  2366. void const * p_config,
  2367. bool use_colors,
  2368. bool log_backend,
  2369. nrf_log_severity_t init_lvl)
  2370. {
  2371. ASSERT(p_cli);
  2372. ret_code_t err_code = nrf_cli_instance_init(p_cli, p_config, use_colors);
  2373. #if NRF_CLI_LOG_BACKEND && NRF_MODULE_ENABLED(NRF_LOG)
  2374. if ((err_code == NRF_SUCCESS) && log_backend && NRF_CLI_LOG_BACKEND)
  2375. {
  2376. int32_t id = nrf_log_backend_add(p_cli->p_log_backend, init_lvl);
  2377. if (id < 0)
  2378. {
  2379. return NRF_ERROR_NO_MEM;
  2380. }
  2381. nrf_log_backend_enable(p_cli->p_log_backend);
  2382. }
  2383. #endif
  2384. return err_code;
  2385. }
  2386. ret_code_t nrf_cli_task_create(nrf_cli_t const * p_cli)
  2387. {
  2388. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  2389. p_cli->p_ctx->task_id = task_create(console_task, p_cli->p_name,(void *)p_cli);
  2390. if (p_cli->p_ctx->task_id == TASK_ID_INVALID)
  2391. {
  2392. return NRF_ERROR_NO_MEM;
  2393. }
  2394. else
  2395. {
  2396. return NRF_SUCCESS;
  2397. }
  2398. #else
  2399. return NRF_ERROR_NOT_SUPPORTED;
  2400. #endif
  2401. }
  2402. static ret_code_t nrf_cli_instance_uninit(nrf_cli_t const * p_cli)
  2403. {
  2404. ASSERT(p_cli);
  2405. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  2406. if (cli_flag_processing_is_set(p_cli))
  2407. {
  2408. return NRF_ERROR_BUSY;
  2409. }
  2410. #if NRF_CLI_LOG_BACKEND && NRF_MODULE_ENABLED(NRF_LOG)
  2411. if (p_cli->p_log_backend != NULL)
  2412. {
  2413. nrf_log_backend_disable(p_cli->p_log_backend);
  2414. nrf_log_backend_remove(p_cli->p_log_backend);
  2415. }
  2416. #endif
  2417. ret_code_t ret = p_cli->p_iface->p_api->uninit(p_cli->p_iface);
  2418. if (ret != NRF_SUCCESS)
  2419. {
  2420. return ret;
  2421. }
  2422. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  2423. history_list_free_memory(p_cli);
  2424. #endif
  2425. memset(p_cli->p_ctx, 0, sizeof(nrf_cli_ctx_t));
  2426. p_cli->p_ctx->state = NRF_CLI_STATE_UNINITIALIZED;
  2427. return NRF_SUCCESS;
  2428. }
  2429. ret_code_t nrf_cli_uninit(nrf_cli_t const * p_cli)
  2430. {
  2431. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  2432. if (cli_flag_processing_is_set(p_cli))
  2433. {
  2434. return NRF_ERROR_BUSY;
  2435. }
  2436. task_events_set(p_cli->p_ctx->task_id, NRF_CLI_KILL_TASK_EVT);
  2437. return NRF_SUCCESS;
  2438. #else
  2439. return nrf_cli_instance_uninit(p_cli);
  2440. #endif
  2441. }
  2442. ret_code_t nrf_cli_start(nrf_cli_t const * p_cli)
  2443. {
  2444. ASSERT(p_cli);
  2445. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  2446. if (p_cli->p_ctx->state != NRF_CLI_STATE_INITIALIZED)
  2447. {
  2448. return NRF_ERROR_INVALID_STATE;
  2449. }
  2450. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  2451. void * p_context = (void *)((uint32_t)task_id_get());
  2452. ((nrf_cli_log_backend_t *)p_cli->p_log_backend->p_ctx)->p_context = p_context;
  2453. #endif
  2454. ret_code_t err_code = p_cli->p_iface->p_api->enable(p_cli->p_iface, false);
  2455. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  2456. task_events_set(task_id_get(), NRF_CLI_TRANSPORT_RX_RDY_TASK_EVT);
  2457. #endif
  2458. if (err_code == NRF_SUCCESS)
  2459. {
  2460. #if NRF_CLI_VT100_COLORS_ENABLED
  2461. vt100_color_set(p_cli, NRF_CLI_NORMAL);
  2462. vt100_bgcolor_set(p_cli, NRF_CLI_VT100_COLOR_BLACK);
  2463. #endif
  2464. nrf_fprintf(p_cli->p_fprintf_ctx, "\n\n");
  2465. cli_state_set(p_cli, NRF_CLI_STATE_ACTIVE);
  2466. }
  2467. return err_code;
  2468. }
  2469. ret_code_t nrf_cli_stop(nrf_cli_t const * p_cli)
  2470. {
  2471. ASSERT(p_cli);
  2472. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  2473. if (p_cli->p_ctx->state == NRF_CLI_STATE_INITIALIZED ||
  2474. p_cli->p_ctx->state == NRF_CLI_STATE_UNINITIALIZED)
  2475. {
  2476. return NRF_ERROR_INVALID_STATE;
  2477. }
  2478. cli_state_set(p_cli, NRF_CLI_STATE_INITIALIZED);
  2479. return NRF_SUCCESS;
  2480. }
  2481. void nrf_cli_process(nrf_cli_t const * p_cli)
  2482. {
  2483. ASSERT(p_cli);
  2484. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  2485. nrf_cli_internal_t internal;
  2486. internal.value = 0;
  2487. internal.flag.processing = 1;
  2488. (void)nrf_atomic_u32_or((nrf_atomic_u32_t *)&p_cli->p_ctx->internal.value,
  2489. internal.value);
  2490. switch (p_cli->p_ctx->state)
  2491. {
  2492. case NRF_CLI_STATE_UNINITIALIZED:
  2493. case NRF_CLI_STATE_INITIALIZED:
  2494. /* Console initialized but not started. */
  2495. break;
  2496. case NRF_CLI_STATE_ACTIVE:
  2497. {
  2498. cli_state_collect(p_cli);
  2499. bool log_processed = cli_log_entry_process(p_cli, false);
  2500. if (log_processed)
  2501. {
  2502. nrf_cli_fprintf(p_cli, NRF_CLI_INFO, "%s", p_cli->p_name);
  2503. if (cli_flag_echo_is_set(p_cli))
  2504. {
  2505. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "%s", p_cli->p_ctx->cmd_buff);
  2506. cursor_position_synchronize(p_cli);
  2507. }
  2508. }
  2509. break;
  2510. }
  2511. default:
  2512. break;
  2513. }
  2514. transport_buffer_flush(p_cli);
  2515. internal.value = (uint32_t)0xFFFFFFFF;
  2516. internal.flag.processing = 0;
  2517. (void)nrf_atomic_u32_and((nrf_atomic_u32_t *)&p_cli->p_ctx->internal.value,
  2518. internal.value);
  2519. }
  2520. /* Function shall be only used by the nrf_fprintf module. */
  2521. void nrf_cli_print_stream(void const * p_user_ctx, char const * p_data, size_t data_len)
  2522. {
  2523. cli_write((nrf_cli_t const *)p_user_ctx,
  2524. p_data,
  2525. data_len,
  2526. NULL);
  2527. }
  2528. void nrf_cli_fprintf(nrf_cli_t const * p_cli,
  2529. nrf_cli_vt100_color_t color,
  2530. char const * p_fmt,
  2531. ...)
  2532. {
  2533. ASSERT(p_fmt);
  2534. ASSERT(p_cli);
  2535. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  2536. va_list args = {0};
  2537. va_start(args, p_fmt);
  2538. #if NRF_MODULE_ENABLED(NRF_CLI_VT100_COLORS)
  2539. if ((p_cli->p_ctx->internal.flag.use_colors) &&
  2540. (color != p_cli->p_ctx->vt100_ctx.col.col))
  2541. {
  2542. nrf_cli_vt100_colors_t col;
  2543. vt100_colors_store(p_cli, &col);
  2544. vt100_color_set(p_cli, color);
  2545. nrf_fprintf_fmt(p_cli->p_fprintf_ctx, p_fmt, &args);
  2546. vt100_colors_restore(p_cli, &col);
  2547. }
  2548. else
  2549. #endif // NRF_MODULE_ENABLED(NRF_CLI_VT100_COLORS)
  2550. {
  2551. nrf_fprintf_fmt(p_cli->p_fprintf_ctx, p_fmt, &args);
  2552. }
  2553. va_end(args);
  2554. }
  2555. /* Function prints a string on terminal screen with requested margin.
  2556. * It takes care to not divide words.
  2557. * p_cli Pointer to CLI instance.
  2558. * p_str Pointer to string to be printed.
  2559. * terminal_offset Requested left margin.
  2560. * offset_first_line Add margin to the first printed line.
  2561. */
  2562. static void format_offset_string_print(nrf_cli_t const * p_cli,
  2563. char const * p_str,
  2564. size_t terminal_offset,
  2565. bool offset_first_line)
  2566. {
  2567. if (p_str == NULL)
  2568. {
  2569. return;
  2570. }
  2571. if (offset_first_line)
  2572. {
  2573. cursor_right_move(p_cli, terminal_offset);
  2574. }
  2575. size_t length;
  2576. size_t offset = 0;
  2577. /* Skipping whitespace. */
  2578. while (isspace((int)*(p_str + offset)))
  2579. {
  2580. ++offset;
  2581. }
  2582. while (1)
  2583. {
  2584. size_t idx = 0;
  2585. length = cli_strlen(p_str) - offset;
  2586. if (length <= p_cli->p_ctx->vt100_ctx.cons.terminal_wid - terminal_offset)
  2587. {
  2588. for (idx = 0; idx < length; idx++)
  2589. {
  2590. if (*(p_str + offset + idx) == '\n')
  2591. {
  2592. transport_buffer_flush(p_cli);
  2593. cli_write(p_cli, p_str + offset, idx, NULL);
  2594. offset += idx + 1;
  2595. cursor_next_line_move(p_cli);
  2596. cursor_right_move(p_cli, terminal_offset);
  2597. break;
  2598. }
  2599. }
  2600. /* String will fit in one line. */
  2601. nrf_fprintf(p_cli->p_fprintf_ctx, p_str + offset);
  2602. break;
  2603. }
  2604. else
  2605. {
  2606. /* String is longer than terminal line so text needs to divide in the way
  2607. to not divide words. */
  2608. length = p_cli->p_ctx->vt100_ctx.cons.terminal_wid - terminal_offset;
  2609. while (1)
  2610. {
  2611. /* Determining line break. */
  2612. if (isspace((int)(*(p_str + offset + idx))))
  2613. {
  2614. length = idx;
  2615. if (*(p_str + offset + idx) == '\n')
  2616. {
  2617. break;
  2618. }
  2619. }
  2620. if ((idx + terminal_offset) >= p_cli->p_ctx->vt100_ctx.cons.terminal_wid)
  2621. {
  2622. /* End of line reached. */
  2623. break;
  2624. }
  2625. ++idx;
  2626. }
  2627. /* Writing one line, fprintf IO buffer must be flushed before calling cli_write. */
  2628. transport_buffer_flush(p_cli);
  2629. cli_write(p_cli, p_str + offset, length, NULL);
  2630. offset += length;
  2631. /* Calculating text offset to ensure that next line will not begin with a space. */
  2632. while (isspace((int)(*(p_str + offset))))
  2633. {
  2634. ++offset;
  2635. }
  2636. cursor_next_line_move(p_cli);
  2637. cursor_right_move(p_cli, terminal_offset);
  2638. }
  2639. }
  2640. cursor_next_line_move(p_cli);
  2641. }
  2642. void nrf_cli_help_print(nrf_cli_t const * p_cli,
  2643. nrf_cli_getopt_option_t const * p_opt,
  2644. size_t opt_len)
  2645. {
  2646. ASSERT(p_cli);
  2647. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  2648. static uint8_t const tab_len = 2;
  2649. static char const opt_sep[] =", "; /* options separator */
  2650. static char const help[] = "-h, --help";
  2651. static char const cmd_sep[] = " - "; /* command separator */
  2652. uint16_t field_width = 0;
  2653. uint16_t longest_string = cli_strlen(help) - cli_strlen(opt_sep);
  2654. /* Printing help string for command. */
  2655. nrf_cli_fprintf(p_cli,
  2656. NRF_CLI_NORMAL,
  2657. "%s%s",
  2658. p_cli->p_ctx->active_cmd.p_syntax,
  2659. cmd_sep);
  2660. field_width = cli_strlen(p_cli->p_ctx->active_cmd.p_syntax) + cli_strlen(cmd_sep);
  2661. format_offset_string_print(p_cli, p_cli->p_ctx->active_cmd.p_help, field_width, false);
  2662. nrf_cli_print(p_cli, "Options:");
  2663. /* Looking for the longest option string. */
  2664. if ((opt_len > 0) && (p_opt != NULL))
  2665. {
  2666. for (size_t i = 0; i < opt_len; ++i)
  2667. {
  2668. if (cli_strlen(p_opt[i].p_optname_short) + cli_strlen(p_opt[i].p_optname)
  2669. > longest_string)
  2670. {
  2671. longest_string = cli_strlen(p_opt[i].p_optname_short)
  2672. + cli_strlen(p_opt[i].p_optname);
  2673. }
  2674. }
  2675. }
  2676. longest_string += cli_strlen(opt_sep) + tab_len;
  2677. nrf_cli_fprintf(p_cli,
  2678. NRF_CLI_NORMAL,
  2679. " %-*s:",
  2680. longest_string,
  2681. help);
  2682. /* Print help string for options (only -h and --help). */
  2683. field_width = longest_string + tab_len + 1; /* tab_len + 1 == " " and ':' from: " %-*s:" */
  2684. format_offset_string_print(p_cli, "Show command help.", field_width, false);
  2685. /* Formating and printing all available options (except -h, --help). */
  2686. if (p_opt != NULL)
  2687. {
  2688. for (size_t i = 0; i < opt_len; ++i)
  2689. {
  2690. if ((p_opt[i].p_optname_short != NULL) && (p_opt[i].p_optname != NULL))
  2691. {
  2692. nrf_cli_fprintf(p_cli,
  2693. NRF_CLI_NORMAL,
  2694. " %s%s%s",
  2695. p_opt[i].p_optname_short,
  2696. opt_sep,
  2697. p_opt[i].p_optname);
  2698. field_width = longest_string + tab_len;
  2699. cursor_right_move(p_cli,
  2700. field_width - ( cli_strlen(p_opt[i].p_optname_short)
  2701. + cli_strlen(p_opt[i].p_optname)
  2702. + tab_len
  2703. + cli_strlen(opt_sep)));
  2704. cli_putc(p_cli, ':');
  2705. ++field_width; /* incrementing because char ':' was already printed above */
  2706. }
  2707. else if (p_opt[i].p_optname_short != NULL)
  2708. {
  2709. nrf_cli_fprintf(p_cli,
  2710. NRF_CLI_NORMAL,
  2711. " %-*s:",
  2712. longest_string,
  2713. p_opt[i].p_optname_short);
  2714. /* tab_len + 1 == " " and ':' from: " %-*s:" */
  2715. field_width = longest_string + tab_len + 1;
  2716. }
  2717. else if (p_opt[i].p_optname != NULL)
  2718. {
  2719. nrf_cli_fprintf(p_cli,
  2720. NRF_CLI_NORMAL,
  2721. " %-*s:",
  2722. longest_string,
  2723. p_opt[i].p_optname);
  2724. /* tab_len + 1 == " " and ':' from: " %-*s:" */
  2725. field_width = longest_string + tab_len + 1;
  2726. }
  2727. else
  2728. {
  2729. /* Do nothing. */
  2730. }
  2731. if (p_opt[i].p_optname_help != NULL)
  2732. {
  2733. format_offset_string_print(p_cli, p_opt[i].p_optname_help, field_width, false);
  2734. }
  2735. else
  2736. {
  2737. cursor_next_line_move(p_cli);
  2738. }
  2739. }
  2740. }
  2741. /* Checking if there are any subcommands avilable. */
  2742. if (p_cli->p_ctx->active_cmd.p_subcmd == NULL)
  2743. {
  2744. return;
  2745. }
  2746. /* Printing formatted help of one level deeper subcommands. */
  2747. nrf_cli_static_entry_t static_entry;
  2748. nrf_cli_cmd_entry_t const * p_cmd = p_cli->p_ctx->active_cmd.p_subcmd;
  2749. nrf_cli_static_entry_t const * p_st_cmd = NULL;
  2750. field_width = 0;
  2751. longest_string = 0;
  2752. size_t cmd_idx = 0;
  2753. /* Searching for the longest subcommand to print. */
  2754. while (1)
  2755. {
  2756. cmd_get(p_cmd, !NRF_CLI_CMD_ROOT_LVL, cmd_idx++, &p_st_cmd, &static_entry);
  2757. if (p_st_cmd == NULL)
  2758. {
  2759. break;
  2760. }
  2761. if (cli_strlen(p_st_cmd->p_syntax) > longest_string)
  2762. {
  2763. longest_string = cli_strlen(p_st_cmd->p_syntax);
  2764. }
  2765. }
  2766. /* Checking if there are dynamic subcommands. */
  2767. if (cmd_idx == 1)
  2768. {
  2769. /* No dynamic subcommands available. */
  2770. return;
  2771. }
  2772. nrf_cli_print(p_cli, "Subcommands:");
  2773. /* Printing subcommands and help string (if exists). */
  2774. cmd_idx = 0;
  2775. while (1)
  2776. {
  2777. cmd_get(p_cmd, !NRF_CLI_CMD_ROOT_LVL, cmd_idx++, &p_st_cmd, &static_entry);
  2778. if (p_st_cmd == NULL)
  2779. {
  2780. break;
  2781. }
  2782. field_width = longest_string + tab_len;
  2783. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL," %-*s:", field_width, p_st_cmd->p_syntax);
  2784. field_width += tab_len + 1; /* tab_len + 1 == " " and ':' from: " %-*s:" */
  2785. if (p_st_cmd->p_help != NULL)
  2786. {
  2787. format_offset_string_print(p_cli, p_st_cmd->p_help, field_width, false);
  2788. }
  2789. else
  2790. {
  2791. cursor_next_line_move(p_cli);
  2792. }
  2793. }
  2794. }
  2795. #if NRF_CLI_LOG_BACKEND && NRF_MODULE_ENABLED(NRF_LOG)
  2796. #define NRF_CLI_LOG_MSG_OVERFLOW_MSK ((uint32_t)7)
  2797. static bool cli_log_entry_process(nrf_cli_t const * p_cli, bool skip)
  2798. {
  2799. nrf_log_entry_t entry;
  2800. #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  2801. bool print_msg = false;
  2802. #endif
  2803. if (nrf_queue_pop(((nrf_cli_log_backend_t *)p_cli->p_log_backend->p_ctx)->p_queue, &entry) !=
  2804. NRF_SUCCESS)
  2805. {
  2806. return false;
  2807. }
  2808. if (skip)
  2809. {
  2810. nrf_memobj_put(entry);
  2811. #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  2812. ++p_cli->p_ctx->statistics.log_lost_cnt;
  2813. if ((p_cli->p_ctx->statistics.log_lost_cnt & NRF_CLI_LOG_MSG_OVERFLOW_MSK) == 1)
  2814. {
  2815. /* Set flag to print a message after clearing the currently entered command. */
  2816. print_msg = true;
  2817. }
  2818. else
  2819. #endif
  2820. {
  2821. return true;
  2822. }
  2823. }
  2824. {
  2825. /* Erasing the currently displayed command and console name. */
  2826. nrf_cli_multiline_cons_t const * p_cons = multiline_console_data_check(p_cli);
  2827. if (p_cons->cur_y > NRF_CLI_INITIAL_CURS_POS)
  2828. {
  2829. cursor_up_move(p_cli, p_cons->cur_y - NRF_CLI_INITIAL_CURS_POS);
  2830. }
  2831. if (p_cons->cur_x > NRF_CLI_INITIAL_CURS_POS)
  2832. {
  2833. cursor_left_move(p_cli, p_cons->cur_x - NRF_CLI_INITIAL_CURS_POS);
  2834. }
  2835. cli_clear_eos(p_cli);
  2836. }
  2837. #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  2838. if (print_msg)
  2839. {
  2840. /* Print the requested string and exit function. */
  2841. nrf_cli_error(p_cli, "Lost logs - increase log backend queue size.");
  2842. return true;
  2843. }
  2844. #endif
  2845. /* Printing logs from the queue. */
  2846. do
  2847. {
  2848. nrf_log_header_t header;
  2849. size_t memobj_offset = 0;
  2850. nrf_log_str_formatter_entry_params_t params;
  2851. nrf_memobj_read(entry, &header, HEADER_SIZE*sizeof(uint32_t), memobj_offset);
  2852. memobj_offset = HEADER_SIZE * sizeof(uint32_t);
  2853. params.timestamp = header.timestamp;
  2854. params.module_id = header.module_id;
  2855. params.dropped = header.dropped;
  2856. params.use_colors = NRF_LOG_USES_COLORS; /* Color will be provided by the console application. */
  2857. if (header.base.generic.type == HEADER_TYPE_STD)
  2858. {
  2859. char const * p_log_str = (char const *)((uint32_t)header.base.std.addr);
  2860. params.severity = (nrf_log_severity_t)header.base.std.severity;
  2861. uint32_t nargs = header.base.std.nargs;
  2862. uint32_t args[6];
  2863. nrf_memobj_read(entry, args, nargs*sizeof(uint32_t), memobj_offset);
  2864. nrf_log_std_entry_process(p_log_str,
  2865. args,
  2866. nargs,
  2867. &params,
  2868. p_cli->p_fprintf_ctx);
  2869. }
  2870. else if (header.base.generic.type == HEADER_TYPE_HEXDUMP)
  2871. {
  2872. uint32_t data_len;
  2873. uint8_t data_buf[8];
  2874. uint32_t chunk_len;
  2875. data_len = header.base.hexdump.len;
  2876. params.severity = (nrf_log_severity_t)header.base.hexdump.severity;
  2877. do
  2878. {
  2879. chunk_len = sizeof(data_buf) > data_len ? data_len : sizeof(data_buf);
  2880. nrf_memobj_read(entry, data_buf, chunk_len, memobj_offset);
  2881. memobj_offset += chunk_len;
  2882. data_len -= chunk_len;
  2883. nrf_log_hexdump_entry_process(data_buf, chunk_len, &params, p_cli->p_fprintf_ctx);
  2884. } while (data_len > 0);
  2885. }
  2886. nrf_memobj_put(entry);
  2887. } while (nrf_queue_pop(((nrf_cli_log_backend_t *)p_cli->p_log_backend->p_ctx)->p_queue, &entry)
  2888. == NRF_SUCCESS);
  2889. return true;
  2890. }
  2891. static void nrf_log_backend_cli_put(nrf_log_backend_t const * p_backend, nrf_log_entry_t * p_msg)
  2892. {
  2893. nrf_cli_log_backend_t * p_backend_cli = (nrf_cli_log_backend_t *)p_backend->p_ctx;
  2894. nrf_cli_t const * p_cli = p_backend_cli->p_cli;
  2895. //If panic mode cannot be handled, stop handling new requests.
  2896. if (p_cli->p_ctx->state != NRF_CLI_STATE_PANIC_MODE_INACTIVE)
  2897. {
  2898. bool panic_mode = (p_cli->p_ctx->state == NRF_CLI_STATE_PANIC_MODE_ACTIVE);
  2899. //If there is no place for a new log entry, remove the oldest one.
  2900. ret_code_t err_code = nrf_queue_push(p_backend_cli->p_queue, &p_msg);
  2901. while (err_code != NRF_SUCCESS)
  2902. {
  2903. (void)cli_log_entry_process(p_cli, panic_mode ? false : true);
  2904. err_code = nrf_queue_push(p_backend_cli->p_queue, &p_msg);
  2905. }
  2906. nrf_memobj_get(p_msg);
  2907. if (panic_mode)
  2908. {
  2909. (void)cli_log_entry_process(p_cli, false);
  2910. }
  2911. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  2912. else
  2913. {
  2914. task_events_set((task_id_t)((uint32_t)p_backend_cli->p_context & 0x000000FF),
  2915. NRF_CLI_LOG_PENDING_TASK_EVT);
  2916. }
  2917. #endif
  2918. }
  2919. }
  2920. static void nrf_log_backend_cli_flush(nrf_log_backend_t const * p_backend)
  2921. {
  2922. nrf_cli_log_backend_t * p_backend_cli = (nrf_cli_log_backend_t *)p_backend->p_ctx;
  2923. nrf_cli_t const * p_cli = p_backend_cli->p_cli;
  2924. nrf_log_entry_t * p_msg;
  2925. if (nrf_queue_pop(p_backend_cli->p_queue, &p_msg) == NRF_SUCCESS)
  2926. {
  2927. (void)cli_log_entry_process(p_cli, false);
  2928. }
  2929. UNUSED_PARAMETER(p_backend);
  2930. }
  2931. static void nrf_log_backend_cli_panic_set(nrf_log_backend_t const * p_backend)
  2932. {
  2933. nrf_cli_log_backend_t * p_backend_cli = (nrf_cli_log_backend_t *)p_backend->p_ctx;
  2934. nrf_cli_t const * p_cli = p_backend_cli->p_cli;
  2935. if (p_cli->p_iface->p_api->enable(p_cli->p_iface, true) == NRF_SUCCESS)
  2936. {
  2937. p_cli->p_ctx->state = NRF_CLI_STATE_PANIC_MODE_ACTIVE;
  2938. }
  2939. else
  2940. {
  2941. p_cli->p_ctx->state = NRF_CLI_STATE_PANIC_MODE_INACTIVE;
  2942. }
  2943. }
  2944. const nrf_log_backend_api_t nrf_log_backend_cli_api = {
  2945. .put = nrf_log_backend_cli_put,
  2946. .flush = nrf_log_backend_cli_flush,
  2947. .panic_set = nrf_log_backend_cli_panic_set,
  2948. };
  2949. #else
  2950. static bool cli_log_entry_process(nrf_cli_t const * p_cli, bool skip)
  2951. {
  2952. UNUSED_PARAMETER(p_cli);
  2953. UNUSED_PARAMETER(skip);
  2954. return false;
  2955. }
  2956. #endif // NRF_CLI_LOG_BACKEND
  2957. /* ============ built-in commands ============ */
  2958. #if NRF_MODULE_ENABLED(NRF_CLI_BUILD_IN_CMDS)
  2959. static bool nrf_cli_build_in_cmd_common_executed(nrf_cli_t const * p_cli,
  2960. bool arg_cnt_nok,
  2961. nrf_cli_getopt_option_t const * p_opt,
  2962. size_t opt_len)
  2963. {
  2964. if (nrf_cli_help_requested(p_cli))
  2965. {
  2966. nrf_cli_help_print(p_cli, p_opt, opt_len);
  2967. return true;
  2968. }
  2969. if (arg_cnt_nok)
  2970. {
  2971. nrf_cli_error(p_cli, "%s: wrong parameter count", p_cli->p_ctx->active_cmd.p_syntax);
  2972. return true;
  2973. }
  2974. return false;
  2975. }
  2976. static void nrf_cli_cmd_clear(nrf_cli_t const * p_cli, size_t argc, char **argv)
  2977. {
  2978. ASSERT(p_cli);
  2979. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  2980. UNUSED_PARAMETER(argv);
  2981. if ((argc == 2) && (nrf_cli_help_requested(p_cli)))
  2982. {
  2983. nrf_cli_help_print(p_cli, NULL, 0);
  2984. return;
  2985. }
  2986. NRF_CLI_VT100_CMD(p_cli, NRF_CLI_VT100_CURSORHOME);
  2987. NRF_CLI_VT100_CMD(p_cli, NRF_CLI_VT100_CLEARSCREEN);
  2988. }
  2989. static void nrf_cli_cmd_cli(nrf_cli_t const * p_cli, size_t argc, char **argv)
  2990. {
  2991. ASSERT(p_cli);
  2992. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  2993. UNUSED_PARAMETER(argv);
  2994. if ((argc == 1) || ((argc == 2) && nrf_cli_help_requested(p_cli)) )
  2995. {
  2996. nrf_cli_help_print(p_cli, NULL, 0);
  2997. return;
  2998. }
  2999. nrf_cli_error(p_cli, NRF_CLI_MSG_SPECIFY_SUBCOMMAND);
  3000. }
  3001. #if NRF_MODULE_ENABLED(NRF_CLI_VT100_COLORS)
  3002. static void nrf_cli_cmd_colors_off(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3003. {
  3004. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc != 1), NULL, 0))
  3005. {
  3006. return;
  3007. }
  3008. p_cli->p_ctx->internal.flag.use_colors = 0;
  3009. }
  3010. static void nrf_cli_cmd_colors_on(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3011. {
  3012. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc != 1), NULL, 0))
  3013. {
  3014. return;
  3015. }
  3016. p_cli->p_ctx->internal.flag.use_colors = 1;
  3017. }
  3018. static void nrf_cli_cmd_colors(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3019. {
  3020. ASSERT(p_cli);
  3021. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  3022. if (argc == 1)
  3023. {
  3024. nrf_cli_help_print(p_cli, NULL, 0);
  3025. return;
  3026. }
  3027. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc != 2), NULL, 0))
  3028. {
  3029. return;
  3030. }
  3031. nrf_cli_error(p_cli, "%s:%s%s", argv[0], NRF_CLI_MSG_UNKNOWN_PARAMETER, argv[1]);
  3032. }
  3033. #endif // NRF_MODULE_ENABLED(NRF_CLI_VT100_COLORS)
  3034. static void nrf_cli_cmd_echo(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3035. {
  3036. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc > 2), NULL, 0))
  3037. {
  3038. return;
  3039. }
  3040. if (argc == 2)
  3041. {
  3042. nrf_cli_error(p_cli, "%s:%s%s", argv[0], NRF_CLI_MSG_UNKNOWN_PARAMETER, argv[1]);
  3043. return;
  3044. }
  3045. nrf_cli_print(p_cli, "Echo status: %s", cli_flag_echo_is_set(p_cli) ? "on" : "off");
  3046. }
  3047. static void nrf_cli_cmd_echo_off(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3048. {
  3049. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc != 1), NULL, 0))
  3050. {
  3051. return;
  3052. }
  3053. cli_flag_echo_clear(p_cli);
  3054. }
  3055. static void nrf_cli_cmd_echo_on(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3056. {
  3057. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc != 1), NULL, 0))
  3058. {
  3059. return;
  3060. }
  3061. cli_flag_echo_set(p_cli);
  3062. }
  3063. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  3064. static void nrf_cli_cmd_history(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3065. {
  3066. ASSERT(p_cli);
  3067. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  3068. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc != 1), NULL, 0))
  3069. {
  3070. return;
  3071. }
  3072. size_t i = 0;
  3073. nrf_memobj_t const * p_cmd_list = p_cli->p_ctx->p_cmd_list_tail;
  3074. nrf_cli_memobj_header_t header;
  3075. while (1)
  3076. {
  3077. if ((p_cmd_list == NULL) || (i >= NRF_CLI_HISTORY_ELEMENT_COUNT))
  3078. {
  3079. break;
  3080. }
  3081. nrf_memobj_read((nrf_memobj_t * )p_cmd_list,
  3082. &header,
  3083. NRF_CLI_HISTORY_HEADER_SIZE,
  3084. 0);
  3085. nrf_memobj_read((nrf_memobj_t * )p_cmd_list,
  3086. p_cli->p_ctx->temp_buff,
  3087. header.cmd_len + 1,
  3088. NRF_CLI_HISTORY_HEADER_SIZE);
  3089. p_cmd_list = header.p_next;
  3090. nrf_cli_print(p_cli, "[%3d] %s", i++, p_cli->p_ctx->temp_buff);
  3091. }
  3092. p_cli->p_ctx->temp_buff[0] = '\0';
  3093. }
  3094. #endif // NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  3095. #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  3096. void nrf_cli_cmd_cli_stats(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3097. {
  3098. if (argc == 1)
  3099. {
  3100. nrf_cli_help_print(p_cli, NULL, 0);
  3101. return;
  3102. }
  3103. if (argc == 2)
  3104. {
  3105. nrf_cli_error(p_cli, "%s:%s%s", argv[0], NRF_CLI_MSG_UNKNOWN_PARAMETER, argv[1]);
  3106. return;
  3107. }
  3108. UNUSED_RETURN_VALUE(nrf_cli_build_in_cmd_common_executed(p_cli, (argc > 2), NULL, 0));
  3109. }
  3110. void nrf_cli_cmd_cli_stats_show(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3111. {
  3112. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc != 1), NULL, 0))
  3113. {
  3114. return;
  3115. }
  3116. nrf_queue_t const * p_queue = ((nrf_cli_log_backend_t *)p_cli->p_log_backend->p_ctx)->p_queue;
  3117. uint8_t max_util = nrf_queue_max_utilization_get(p_queue);
  3118. uint8_t utilization = (uint8_t)(max_util * 100ul / p_queue->size);
  3119. nrf_cli_print(p_cli,
  3120. "Lost logs: %u\nMax log queue utilization: %u%% [%u/%u]",
  3121. p_cli->p_ctx->statistics.log_lost_cnt,
  3122. utilization,
  3123. max_util,
  3124. p_queue->size);
  3125. }
  3126. void nrf_cli_cmd_cli_stats_reset(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3127. {
  3128. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc != 1), NULL, 0))
  3129. {
  3130. return;
  3131. }
  3132. p_cli->p_ctx->statistics.log_lost_cnt = 0;
  3133. nrf_queue_max_utilization_reset(
  3134. ((nrf_cli_log_backend_t *)p_cli->p_log_backend->p_ctx)->p_queue);
  3135. }
  3136. #endif // NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  3137. static void nrf_cli_cmd_resize_default(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3138. {
  3139. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc != 1), NULL, 0))
  3140. {
  3141. return;
  3142. }
  3143. NRF_CLI_VT100_CMD(p_cli, NRF_CLI_VT100_SETCOL_80);
  3144. p_cli->p_ctx->vt100_ctx.cons.terminal_wid = NRF_CLI_DEFAULT_TERMINAL_WIDTH;
  3145. p_cli->p_ctx->vt100_ctx.cons.terminal_hei = NRF_CLI_DEFAULT_TERMINAL_HEIGHT;
  3146. }
  3147. static void nrf_cli_cmd_resize(nrf_cli_t const * p_cli, size_t argc, char **argv)
  3148. {
  3149. ASSERT(p_cli);
  3150. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  3151. if (argc == 1)
  3152. {
  3153. if (terminal_size_get(p_cli,
  3154. &p_cli->p_ctx->vt100_ctx.cons.terminal_wid,
  3155. &p_cli->p_ctx->vt100_ctx.cons.terminal_hei) != NRF_SUCCESS)
  3156. {
  3157. p_cli->p_ctx->vt100_ctx.cons.terminal_wid = NRF_CLI_DEFAULT_TERMINAL_WIDTH;
  3158. p_cli->p_ctx->vt100_ctx.cons.terminal_hei = NRF_CLI_DEFAULT_TERMINAL_HEIGHT;
  3159. nrf_cli_warn(p_cli, "No response from the terminal, assumed 80x24 screen size");
  3160. }
  3161. return;
  3162. }
  3163. if (nrf_cli_build_in_cmd_common_executed(p_cli, (argc > 2), NULL, 0))
  3164. {
  3165. return;
  3166. }
  3167. nrf_cli_error(p_cli, "%s:%s%s", argv[0], NRF_CLI_MSG_UNKNOWN_PARAMETER, argv[1]);
  3168. }
  3169. #if NRF_MODULE_ENABLED(NRF_CLI_VT100_COLORS)
  3170. NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_colors)
  3171. {
  3172. NRF_CLI_CMD(off, NULL, NRF_CLI_HELP_COLORS_OFF, nrf_cli_cmd_colors_off),
  3173. NRF_CLI_CMD(on, NULL, NRF_CLI_HELP_COLORS_ON, nrf_cli_cmd_colors_on),
  3174. NRF_CLI_SUBCMD_SET_END
  3175. };
  3176. #endif
  3177. NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_echo)
  3178. {
  3179. NRF_CLI_CMD(off, NULL, NRF_CLI_HELP_ECHO_OFF, nrf_cli_cmd_echo_off),
  3180. NRF_CLI_CMD(on, NULL, NRF_CLI_HELP_ECHO_ON, nrf_cli_cmd_echo_on),
  3181. NRF_CLI_SUBCMD_SET_END
  3182. };
  3183. #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  3184. NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_cli_stats)
  3185. {
  3186. NRF_CLI_CMD(reset, NULL, NRF_CLI_HELP_STATISTICS_RESET, nrf_cli_cmd_cli_stats_reset),
  3187. NRF_CLI_CMD(show, NULL, NRF_CLI_HELP_STATISTICS_SHOW, nrf_cli_cmd_cli_stats_show),
  3188. NRF_CLI_SUBCMD_SET_END
  3189. };
  3190. #endif // NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  3191. NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_cli)
  3192. {
  3193. #if NRF_MODULE_ENABLED(NRF_CLI_VT100_COLORS)
  3194. NRF_CLI_CMD(colors, &m_sub_colors, NRF_CLI_HELP_COLORS, nrf_cli_cmd_colors),
  3195. #endif
  3196. NRF_CLI_CMD(echo, &m_sub_echo, NRF_CLI_HELP_ECHO, nrf_cli_cmd_echo),
  3197. #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  3198. NRF_CLI_CMD(stats, &m_sub_cli_stats, NRF_CLI_HELP_STATISTICS, nrf_cli_cmd_cli_stats),
  3199. #endif
  3200. NRF_CLI_SUBCMD_SET_END
  3201. };
  3202. NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_resize)
  3203. {
  3204. NRF_CLI_CMD(default, NULL, NRF_CLI_HELP_RESIZE_DEFAULT, nrf_cli_cmd_resize_default),
  3205. NRF_CLI_SUBCMD_SET_END
  3206. };
  3207. NRF_CLI_CMD_REGISTER(clear, NULL, NRF_CLI_HELP_CLEAR, nrf_cli_cmd_clear);
  3208. NRF_CLI_CMD_REGISTER(cli, &m_sub_cli, NRF_CLI_HELP_CLI, nrf_cli_cmd_cli);
  3209. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  3210. NRF_CLI_CMD_REGISTER(history, NULL, NRF_CLI_HELP_HISTORY, nrf_cli_cmd_history);
  3211. #endif
  3212. NRF_CLI_CMD_REGISTER(resize, &m_sub_resize, NRF_CLI_HELP_RESIZE, nrf_cli_cmd_resize);
  3213. #endif // NRF_MODULE_ENABLED(NRF_CLI_BUILD_IN_CMDS)
  3214. #endif // NRF_MODULE_ENABLED(NRF_CLI)