5c23e84f701229a4a09f23a4d4c4b3802badeb4f
[platform/core/uifw/dali-adaptor.git] / dali / internal / network / common / network-performance-protocol.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // INTERNAL INCLUDES
19
20 // CLASS HEADER
21 #include <dali/internal/network/common/network-performance-protocol.h>
22
23 // EXTERNAL INCLUDES
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 namespace Dali
29 {
30 namespace PerformanceProtocol
31 {
32 namespace
33 {
34 /**
35  * Command parameter type
36  */
37 enum PARAMETER_TYPE
38 {
39   NO_PARAMS,
40   UNSIGNED_INT,
41   STRING
42 };
43
44 /**
45  * Command information structure
46  */
47 struct CommandInfo
48 {
49   CommandId      cmdId;
50   CommandString  cmdString;
51   PARAMETER_TYPE paramType;
52 };
53
54 /**
55  * Command lookup table
56  */
57 // clang-format off
58 CommandInfo CommandLookup[]=
59 {
60   {HELP_MESSAGE,                "help",           NO_PARAMS   },
61   {ENABLE_METRIC,               "enable_metric",  UNSIGNED_INT},
62   {DISABLE_METRIC,              "disable_metric", UNSIGNED_INT},
63   {LIST_METRICS_AVAILABLE,      "list_metrics",   NO_PARAMS   },
64   {ENABLE_TIME_MARKER_BIT_MASK, "set_marker",     UNSIGNED_INT},
65   {DUMP_SCENE_GRAPH,            "dump_scene",     NO_PARAMS   },
66   {SET_PROPERTIES,              "set_properties", STRING      },
67   {UNKNOWN_COMMAND,             "unknown",        NO_PARAMS   }
68 };
69 // clang-format on
70 const unsigned int CommandLookupLength = sizeof(CommandLookup) / sizeof(CommandInfo);
71
72 #define GREEN "\033[01;32m"
73 #define NORMAL "\e[m"
74 #define PARAM "\033[22;32m"
75 #define YELLOW "\033[01;33m"
76
77 // clang-format off
78 const char* const helpMsg =
79     YELLOW
80     "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
81     "  Dali performance console                           \n"
82     "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" NORMAL
83     GREEN " list_metrics " NORMAL " - list available metrics\n"
84     GREEN " enable_metric " PARAM " metricId" NORMAL " - enable a metric \n"
85     GREEN " disable_metric " PARAM " metricId" NORMAL " - disable a metric\n\n"
86     GREEN " set_marker " PARAM " value " NORMAL "-output Dali markers\n"
87     "            : Bit 0  = V_SYNC (1)\n"
88     "            : Bit 1  = Update task (2)\n"
89     "            : Bit 2  = Render task (4) \n"
90     "            : Bit 3  = Event Processing task (8)\n"
91     "            : Bit 4  = SwapBuffers (16)\n"
92     "            : Bit 5  = Life cycle events  (32)\n"
93     "            : Bit 6  = Resource event (64)\n"
94     "\n"
95     GREEN " set_properties " NORMAL " - set an actor property command. Format:\n\n"
96     GREEN " set_properties " PARAM "|ActorIndex;Property;Value|" NORMAL ", e.g: \n"
97     GREEN " set_properties " PARAM "|178;Size;[ 144.0, 144.0, 144.0 ]|178;Color;[ 1.0, 1,0, 1.0 ]|\n"
98     "\n"
99     GREEN " dump_scene" NORMAL " - dump the current scene in json format\n";
100 // clang-format off
101 } // un-named namespace
102
103 bool GetCommandId( const char* const commandString, unsigned int lengthInBytes, CommandId& commandId, unsigned int& intParam, std::string& stringParam  )
104 {
105   commandId = UNKNOWN_COMMAND;
106   intParam = 0;
107
108   // the command list is small so just do a O(n) search for the commandID.
109   for( unsigned int i = 0 ; i < CommandLookupLength; ++i )
110   {
111     if( strncmp( commandString, CommandLookup[i].cmdString ,strlen(CommandLookup[i].cmdString  )) == 0 )
112     {
113       commandId = CommandLookup[i].cmdId;
114
115       // if the command has a parameter read it
116       if( CommandLookup[i].paramType ==  UNSIGNED_INT)
117       {
118         int count = sscanf(commandString,"%*s %d",&intParam);
119         if( count != 1 )
120         {
121           // missing parameter
122           return false;
123         }
124       }
125       else if (CommandLookup[i].paramType == STRING )
126       {
127         char* charParam( NULL );
128         // allocates the character array
129         int count = sscanf(commandString,"%*s %ms",&charParam);
130         if( count != 1 )
131         {
132           // missing parameter
133           return false;
134         }
135         stringParam = std::string( charParam);
136         free(charParam);
137       }
138       return true;
139     }
140   }
141   // not found
142   return false;
143 }
144
145 bool GetCommandString( CommandId commandId, CommandString& commandString )
146 {
147   for( unsigned int i = 0; i < CommandLookupLength; ++i)
148   {
149     if( CommandLookup[ i ].cmdId == commandId )
150     {
151       strncpy( commandString,  CommandLookup[ i ].cmdString, strlen(CommandLookup[ i ].cmdString) );
152       return true;
153     }
154   }
155   strncpy( commandString, CommandLookup[ UNKNOWN_COMMAND ].cmdString, MAX_COMMAND_STRING_LENGTH);
156   return false;
157 }
158
159 const char* const GetHelpMessage()
160 {
161   return helpMsg;
162 }
163
164
165 } // namespace PerformanceProtocol
166
167 } // namespace Dali