1*02ca8ccaSAndroid Build Coastguard WorkerAttaching External Custom Decoders {#custom_decoders} 2*02ca8ccaSAndroid Build Coastguard Worker================================== 3*02ca8ccaSAndroid Build Coastguard Worker 4*02ca8ccaSAndroid Build Coastguard Worker@brief A description of the C API external decoder interface. 5*02ca8ccaSAndroid Build Coastguard Worker 6*02ca8ccaSAndroid Build Coastguard WorkerIntroduction 7*02ca8ccaSAndroid Build Coastguard Worker------------ 8*02ca8ccaSAndroid Build Coastguard Worker 9*02ca8ccaSAndroid Build Coastguard WorkerAn external custom decoder is one which decodes a CoreSight trace byte stream from a source other 10*02ca8ccaSAndroid Build Coastguard Workerthan an ARM core which cannot be decoded by the standard built-in decoders within the library. 11*02ca8ccaSAndroid Build Coastguard Worker 12*02ca8ccaSAndroid Build Coastguard WorkerAn example of this may be a trace stream from a DSP device. 13*02ca8ccaSAndroid Build Coastguard Worker 14*02ca8ccaSAndroid Build Coastguard WorkerThe external decoder API allows a suitable decoder to be attached to the library and used in the 15*02ca8ccaSAndroid Build Coastguard Workersame way as the built-in decoders. This means that the external decoder can be created and destroyed 16*02ca8ccaSAndroid Build Coastguard Workerusing the decode tree API, and will integrate seamlessly with any ARM processor decoders that are part 17*02ca8ccaSAndroid Build Coastguard Workerof the same tree. 18*02ca8ccaSAndroid Build Coastguard Worker 19*02ca8ccaSAndroid Build Coastguard WorkerAn external decoder will be required to use three standard structures:- 20*02ca8ccaSAndroid Build Coastguard Worker 21*02ca8ccaSAndroid Build Coastguard Worker- `ocsd_extern_dcd_fact_t` : This is a decoder "factory" that allows the creation of the custom decoders. 22*02ca8ccaSAndroid Build Coastguard Worker- `ocsd_extern_dcd_inst_t` : This structure provides decoder data to the library for a single decoder instance. 23*02ca8ccaSAndroid Build Coastguard Worker- `ocsd_extern_dcd_cb_fns` : This structure provides a set of callback functions allowing the decoder to use library functionality in the same way as built-in decoders. 24*02ca8ccaSAndroid Build Coastguard Worker 25*02ca8ccaSAndroid Build Coastguard WorkerThese structures consist of data and function pointers to allow integration with the library infrastructure. 26*02ca8ccaSAndroid Build Coastguard Worker 27*02ca8ccaSAndroid Build Coastguard WorkerRegistering A Decoder 28*02ca8ccaSAndroid Build Coastguard Worker--------------------- 29*02ca8ccaSAndroid Build Coastguard Worker 30*02ca8ccaSAndroid Build Coastguard WorkerA single API function is provided to allow a decoder to be registered with the library by name. 31*02ca8ccaSAndroid Build Coastguard Worker 32*02ca8ccaSAndroid Build Coastguard Worker ocsd_err_t ocsd_register_custom_decoder(const char *name, ocsd_extern_dcd_fact_t *p_dcd_fact); 33*02ca8ccaSAndroid Build Coastguard Worker 34*02ca8ccaSAndroid Build Coastguard WorkerThis registers the custom decoder with the library using the supplied name and factory structure. 35*02ca8ccaSAndroid Build Coastguard WorkerAs part of the registration function the custom decoder will be assigned a protocol ID which may be used in 36*02ca8ccaSAndroid Build Coastguard WorkerAPI functions requiring this parameter. 37*02ca8ccaSAndroid Build Coastguard Worker 38*02ca8ccaSAndroid Build Coastguard WorkerOnce registered, the standard API functions used with the built-in decoders will work with the custom decoder. 39*02ca8ccaSAndroid Build Coastguard Worker 40*02ca8ccaSAndroid Build Coastguard WorkerThe Factory Structure 41*02ca8ccaSAndroid Build Coastguard Worker--------------------- 42*02ca8ccaSAndroid Build Coastguard WorkerThis structure contains the interface that is registered with the library to allow the creation of custom decoder instances. 43*02ca8ccaSAndroid Build Coastguard Worker 44*02ca8ccaSAndroid Build Coastguard WorkerThe mandatory functions that must be provided include: 45*02ca8ccaSAndroid Build Coastguard Worker- `fnCreateCustomDecoder` : Creates a decoder. This function will fill in a `ocsd_extern_dcd_inst_t` structure for the decoder instance. 46*02ca8ccaSAndroid Build Coastguard Worker- `fnDestroyCustomDecoder` : Destroys the decoder. Takes the `decoder_handle` attribute of the instance structure. 47*02ca8ccaSAndroid Build Coastguard Worker- `fnGetCSIDFromConfig` : Extracts the CoreSight Trace ID from the decoder configuration structure. 48*02ca8ccaSAndroid Build Coastguard Worker May be called before the create function. The CSID is used as part of the creation process to 49*02ca8ccaSAndroid Build Coastguard Worker attach the decoder to the correct trace byte stream. 50*02ca8ccaSAndroid Build Coastguard Worker 51*02ca8ccaSAndroid Build Coastguard Worker`fnPacketToString` : This optional function will provide a human readable string from a protocol specific packet structure. 52*02ca8ccaSAndroid Build Coastguard Worker 53*02ca8ccaSAndroid Build Coastguard Worker`protocol_id` : This is filled in when the decoder type is registered with the library. Used in some API 54*02ca8ccaSAndroid Build Coastguard Worker calls to specify the decoder protocol type. 55*02ca8ccaSAndroid Build Coastguard Worker 56*02ca8ccaSAndroid Build Coastguard Worker 57*02ca8ccaSAndroid Build Coastguard Worker 58*02ca8ccaSAndroid Build Coastguard WorkerCreating a Custom Decoder Instance 59*02ca8ccaSAndroid Build Coastguard Worker---------------------------------- 60*02ca8ccaSAndroid Build Coastguard Worker 61*02ca8ccaSAndroid Build Coastguard WorkerOnce the custom decoder factory has been registered with the library then using the decoder uses the standard creation API:- 62*02ca8ccaSAndroid Build Coastguard Worker 63*02ca8ccaSAndroid Build Coastguard Worker`ocsd_dt_create_decoder(const dcd_tree_handle_t handle, const char *decoder_name, const int create_flags, 64*02ca8ccaSAndroid Build Coastguard Worker const void *decoder_cfg, unsigned char *pCSID)` 65*02ca8ccaSAndroid Build Coastguard Worker 66*02ca8ccaSAndroid Build Coastguard Worker 67*02ca8ccaSAndroid Build Coastguard WorkerThis creates a decoder by type name in the current decode tree and attaches it to the trace data stream associated with a CoreSight trace ID extracted from 68*02ca8ccaSAndroid Build Coastguard Workerthe trace configuration. 69*02ca8ccaSAndroid Build Coastguard Worker 70*02ca8ccaSAndroid Build Coastguard WorkerTo create a custom decoder instance simply use the custom name and a pointer to the custom configuration structure. 71*02ca8ccaSAndroid Build Coastguard Worker 72*02ca8ccaSAndroid Build Coastguard WorkerCalling this on a custom decoder name will result in a call to the factor function `fnCreateCustomDecoder` function:- 73*02ca8ccaSAndroid Build Coastguard Worker`ocsd_err_t CreateCustomDecoder(const int create_flags, const void *decoder_cfg, const ocsd_extern_dcd_cb_fns *p_lib_callbacks, ocsd_extern_dcd_inst_t *p_decoder_inst)` 74*02ca8ccaSAndroid Build Coastguard Worker 75*02ca8ccaSAndroid Build Coastguard WorkerThis will first require that the `ocsd_extern_dcd_inst_t` structure is populated. 76*02ca8ccaSAndroid Build Coastguard Worker 77*02ca8ccaSAndroid Build Coastguard WorkerThere is are two mandatory function calls in this structure that may be called by the library 78*02ca8ccaSAndroid Build Coastguard Worker 79*02ca8ccaSAndroid Build Coastguard Worker `fnTraceDataIn` : the decoder must provide this as this is called by the library to provide the 80*02ca8ccaSAndroid Build Coastguard Worker raw trace data to the decoder. 81*02ca8ccaSAndroid Build Coastguard Worker 82*02ca8ccaSAndroid Build Coastguard Worker `fn_update_pkt_mon` : Allows the library to communicate when packet sink / packet monitor interfaces are attached to the decoder and in use. 83*02ca8ccaSAndroid Build Coastguard Worker 84*02ca8ccaSAndroid Build Coastguard WorkerThe decoder creation process will also fill in the additional information to allow the library to correctly call back into the custom decoder using the `decoder_handle` parameter. 85*02ca8ccaSAndroid Build Coastguard Worker 86*02ca8ccaSAndroid Build Coastguard WorkerSecondly the library will provide a structure of callback functions - `ocsd_extern_dcd_cb_fns` - that the decoder can use to access standard library functionality. 87*02ca8ccaSAndroid Build Coastguard WorkerThis includes the standard error and message logging functions, the memory access and ARM instruction decode functions, plus the current output sink for generic 88*02ca8ccaSAndroid Build Coastguard Workertrace elements generated by the decoder. The decoder is not required to use these functions - indeed the ARM instruction decode will not be useful to none ARM 89*02ca8ccaSAndroid Build Coastguard Workerarchitecture decoders, but should where possible use these functions if being used as part of a combined ARM / custom decoder tree. This will simplify client 90*02ca8ccaSAndroid Build Coastguard Workeruse of the external decoders. 91*02ca8ccaSAndroid Build Coastguard Worker 92*02ca8ccaSAndroid Build Coastguard WorkerThe `create_flags` parameter will describe the expected operational mode for the decoder. The flags are:- 93*02ca8ccaSAndroid Build Coastguard Worker- `OCSD_CREATE_FLG_PACKET_PROC` : Packet processing only - the decoder will split the incoming stream into protocol trace packets and output these. 94*02ca8ccaSAndroid Build Coastguard Worker- `OCSD_CREATE_FLG_FULL_DECODER` : Full decode - the decoder will split the incoming stream into protocol trace packets and further decode and analyse these to produce generic trace output which may describe the program flow. 95*02ca8ccaSAndroid Build Coastguard Worker 96*02ca8ccaSAndroid Build Coastguard WorkerFinally the decoder creation function will interpret the custom configuration (`decoder_cfg`) and fill in the CoreSight Trace ID parameter `pCSID` 97*02ca8ccaSAndroid Build Coastguard Workerfor this decoder instance. Decoder configuration structures describe registers and parameters used in programming up the trace source. The only 98*02ca8ccaSAndroid Build Coastguard Workerminimum requirement is that it is possible to extract a CoreSight trace ID from the configuration to allow the library to attach the correct byte 99*02ca8ccaSAndroid Build Coastguard Workerstream to the decoder. 100*02ca8ccaSAndroid Build Coastguard Worker 101*02ca8ccaSAndroid Build Coastguard Worker 102*02ca8ccaSAndroid Build Coastguard WorkerExample : The echo_test decoder 103*02ca8ccaSAndroid Build Coastguard Worker-------------------------------- 104*02ca8ccaSAndroid Build Coastguard Worker 105*02ca8ccaSAndroid Build Coastguard WorkerThe echo_test decoder is provided to both test the C-API interfaces provided for using custom decoders and as a worked example for using these interfaces. 106*02ca8ccaSAndroid Build Coastguard Worker 107*02ca8ccaSAndroid Build Coastguard WorkerThis decoder is initialised and created by the `c_api_pkt_print_test` program when the `-extern` command line option is used. 108*02ca8ccaSAndroid Build Coastguard Worker 109*02ca8ccaSAndroid Build Coastguard WorkerIn order to use a custom decoder, the header files for that decoder must be included by the client as they are not part of the built-in provided by the standard library includes. 110*02ca8ccaSAndroid Build Coastguard Worker 111*02ca8ccaSAndroid Build Coastguard Worker #include "ext_dcd_echo_test_fact.h" // provides the ext_echo_get_dcd_fact() fn 112*02ca8ccaSAndroid Build Coastguard Worker #include "ext_dcd_echo_test.h" // provides the echo_dcd_cfg_t config structure. 113*02ca8ccaSAndroid Build Coastguard Worker 114*02ca8ccaSAndroid Build Coastguard WorkerThe `register_extern_decoder()` function in the test shows how simple the API is to use. 115*02ca8ccaSAndroid Build Coastguard Worker 116*02ca8ccaSAndroid Build Coastguard WorkerThe implementation of the decoder provides an external function to get a factory structure. 117*02ca8ccaSAndroid Build Coastguard Worker 118*02ca8ccaSAndroid Build Coastguard Worker p_ext_fact = ext_echo_get_dcd_fact(); 119*02ca8ccaSAndroid Build Coastguard Worker 120*02ca8ccaSAndroid Build Coastguard WorkerAssuming this returns a structure then the decoder is registered by name. 121*02ca8ccaSAndroid Build Coastguard Worker 122*02ca8ccaSAndroid Build Coastguard Worker if (p_ext_fact) 123*02ca8ccaSAndroid Build Coastguard Worker { 124*02ca8ccaSAndroid Build Coastguard Worker err = ocsd_register_custom_decoder(EXT_DCD_NAME, p_ext_fact); 125*02ca8ccaSAndroid Build Coastguard Worker } 126*02ca8ccaSAndroid Build Coastguard Worker 127*02ca8ccaSAndroid Build Coastguard WorkerAfter this the test uses the same code path as the built in decoders when testing the custom decoder. 128*02ca8ccaSAndroid Build Coastguard WorkerThe test function `ocsd_err_t create_decoder_extern(dcd_tree_handle_t dcd_tree_h)` is called if the test parameters indicate a custom decoder is needed. 129*02ca8ccaSAndroid Build Coastguard WorkerThis populates the custom configuration structure specific to the echo_test decoder (`echo_dcd_cfg_t`), then passes this plus the decoder name to the same `create_generic_decoder()` function used when testing the built in decoders. 130*02ca8ccaSAndroid Build Coastguard Worker 131*02ca8ccaSAndroid Build Coastguard Worker 132*02ca8ccaSAndroid Build Coastguard Worker static ocsd_err_t create_decoder_extern(dcd_tree_handle_t dcd_tree_h) 133*02ca8ccaSAndroid Build Coastguard Worker { 134*02ca8ccaSAndroid Build Coastguard Worker echo_dcd_cfg_t trace_cfg_ext; 135*02ca8ccaSAndroid Build Coastguard Worker 136*02ca8ccaSAndroid Build Coastguard Worker /* setup the custom configuration */ 137*02ca8ccaSAndroid Build Coastguard Worker trace_cfg_ext.cs_id = 0x010; 138*02ca8ccaSAndroid Build Coastguard Worker if (test_trc_id_override != 0) 139*02ca8ccaSAndroid Build Coastguard Worker { 140*02ca8ccaSAndroid Build Coastguard Worker trace_cfg_ext.cs_id = (uint32_t)test_trc_id_override; 141*02ca8ccaSAndroid Build Coastguard Worker } 142*02ca8ccaSAndroid Build Coastguard Worker 143*02ca8ccaSAndroid Build Coastguard Worker /* create an external decoder - no context needed as we have a single stream to a single handler. */ 144*02ca8ccaSAndroid Build Coastguard Worker return create_generic_decoder(dcd_tree_h, EXT_DCD_NAME, (void *)&trace_cfg_ext, 0); 145*02ca8ccaSAndroid Build Coastguard Worker } 146*02ca8ccaSAndroid Build Coastguard Worker 147*02ca8ccaSAndroid Build Coastguard WorkerFrom the test program perspective, these are the only changes made to the test program to test this decoder. 148*02ca8ccaSAndroid Build Coastguard WorkerThe `create_generic_decoder()` then uses the normal C-API calls such as `ocsd_dt_create_decoder()` and `ocsd_dt_attach_packet_callback()` to hook the decoder into the decode tree infrastructure. 149