| 1 |
4 |
sergeykhbr |
/*
|
| 2 |
|
|
* Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
|
| 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 |
|
|
#ifndef __DEBUGGER_COMMON_ISERVICE_H__
|
| 18 |
|
|
#define __DEBUGGER_COMMON_ISERVICE_H__
|
| 19 |
|
|
|
| 20 |
|
|
#include <iface.h>
|
| 21 |
|
|
#include <attribute.h>
|
| 22 |
|
|
#include <api_utils.h>
|
| 23 |
|
|
#include "coreservices/imemop.h"
|
| 24 |
|
|
|
| 25 |
|
|
namespace debugger {
|
| 26 |
|
|
|
| 27 |
|
|
static const char *const IFACE_SERVICE = "IService";
|
| 28 |
|
|
|
| 29 |
|
|
class IService : public IFace {
|
| 30 |
|
|
public:
|
| 31 |
|
|
explicit IService(const char *obj_name) : IFace(IFACE_SERVICE) {
|
| 32 |
|
|
listInterfaces_ = AttributeType(Attr_List);
|
| 33 |
|
|
listAttributes_ = AttributeType(Attr_List);
|
| 34 |
|
|
listPorts_ = AttributeType(Attr_List);
|
| 35 |
|
|
registerInterface(static_cast<IService *>(this));
|
| 36 |
|
|
registerAttribute("LogLevel", &logLevel_);
|
| 37 |
|
|
obj_name_.make_string(obj_name);
|
| 38 |
|
|
logLevel_.make_int64(LOG_ERROR);
|
| 39 |
|
|
}
|
| 40 |
|
|
virtual ~IService() {
|
| 41 |
|
|
// @warning: NEED to unregister attribute from class destructor
|
| 42 |
|
|
/*for (unsigned i = 0; i < listAttributes_.size(); i++) {
|
| 43 |
|
|
IAttribute *iattr = static_cast<IAttribute *>(
|
| 44 |
|
|
listAttributes_[i].to_iface());
|
| 45 |
|
|
iattr->freeAttrName();
|
| 46 |
|
|
iattr->freeAttrDescription();
|
| 47 |
|
|
}*/
|
| 48 |
|
|
}
|
| 49 |
|
|
|
| 50 |
|
|
virtual void initService(const AttributeType *args) {
|
| 51 |
|
|
if (!args || !args->is_list()) {
|
| 52 |
|
|
return;
|
| 53 |
|
|
}
|
| 54 |
|
|
AttributeType *cur_attr;
|
| 55 |
|
|
for (unsigned i = 0; i < args->size(); i++) {
|
| 56 |
|
|
const AttributeType &item = (*args)[i];
|
| 57 |
|
|
if (item.size() < 2 || !item[0u].is_string()) {
|
| 58 |
|
|
continue;
|
| 59 |
|
|
}
|
| 60 |
|
|
cur_attr = static_cast<AttributeType *>(
|
| 61 |
|
|
getAttribute(item[0u].to_string()));
|
| 62 |
|
|
if (cur_attr == NULL) {
|
| 63 |
|
|
RISCV_error("Attribute '%s' not found", item[0u].to_string());
|
| 64 |
|
|
continue;
|
| 65 |
|
|
}
|
| 66 |
|
|
(*cur_attr) = item[1];
|
| 67 |
|
|
if (item.size() >= 3 && item[2].is_string()) {
|
| 68 |
|
|
static_cast<IAttribute *>(cur_attr)->allocAttrDescription(
|
| 69 |
|
|
item[2].to_string());
|
| 70 |
|
|
}
|
| 71 |
|
|
}
|
| 72 |
|
|
}
|
| 73 |
|
|
|
| 74 |
|
|
virtual void postinitService() {}
|
| 75 |
|
|
virtual void predeleteService() {}
|
| 76 |
|
|
|
| 77 |
|
|
virtual void registerInterface(IFace *iface) {
|
| 78 |
|
|
AttributeType item(iface);
|
| 79 |
|
|
listInterfaces_.add_to_list(&item);
|
| 80 |
|
|
if (strcmp(iface->getFaceName(), IFACE_MEMORY_OPERATION) == 0) {
|
| 81 |
|
|
IMemoryOperation *imemop = static_cast<IMemoryOperation *>(iface);
|
| 82 |
|
|
registerAttribute("MapList", &imemop->listMap_);
|
| 83 |
|
|
registerAttribute("BaseAddress", &imemop->baseAddress_);
|
| 84 |
|
|
registerAttribute("Length", &imemop->length_);
|
| 85 |
|
|
registerAttribute("Priority", &imemop->priority_);
|
| 86 |
|
|
}
|
| 87 |
|
|
}
|
| 88 |
|
|
virtual void registerPortInterface(const char *portname, IFace *iface) {
|
| 89 |
|
|
AttributeType item;
|
| 90 |
|
|
item.make_list(2);
|
| 91 |
|
|
item[0u].make_string(portname);
|
| 92 |
|
|
item[1].make_iface(iface);
|
| 93 |
|
|
listPorts_.add_to_list(&item);
|
| 94 |
|
|
}
|
| 95 |
|
|
|
| 96 |
|
|
virtual void unregisterInterface(IFace *iface) {
|
| 97 |
|
|
for (unsigned i = 0; i < listInterfaces_.size(); i++) {
|
| 98 |
|
|
if (listInterfaces_[i].to_iface() == iface) {
|
| 99 |
|
|
listInterfaces_.remove_from_list(i);
|
| 100 |
|
|
break;
|
| 101 |
|
|
}
|
| 102 |
|
|
}
|
| 103 |
|
|
}
|
| 104 |
|
|
|
| 105 |
|
|
virtual IFace *getInterface(const char *name) {
|
| 106 |
|
|
IFace *tmp;
|
| 107 |
|
|
for (unsigned i = 0; i < listInterfaces_.size(); i++) {
|
| 108 |
|
|
tmp = listInterfaces_[i].to_iface();
|
| 109 |
|
|
if (strcmp(name, tmp->getFaceName()) == 0) {
|
| 110 |
|
|
return tmp;
|
| 111 |
|
|
}
|
| 112 |
|
|
}
|
| 113 |
|
|
return NULL;
|
| 114 |
|
|
}
|
| 115 |
|
|
|
| 116 |
|
|
virtual IFace *getPortInterface(const char *portname,
|
| 117 |
|
|
const char *facename) {
|
| 118 |
|
|
IFace *tmp;
|
| 119 |
|
|
for (unsigned i = 0; i < listPorts_.size(); i++) {
|
| 120 |
|
|
AttributeType &item = listPorts_[i];
|
| 121 |
|
|
if (!item[0u].is_equal(portname)) {
|
| 122 |
|
|
continue;
|
| 123 |
|
|
}
|
| 124 |
|
|
tmp = item[1].to_iface();
|
| 125 |
|
|
if (strcmp(facename, tmp->getFaceName()) == 0) {
|
| 126 |
|
|
return tmp;
|
| 127 |
|
|
}
|
| 128 |
|
|
}
|
| 129 |
|
|
return NULL;
|
| 130 |
|
|
}
|
| 131 |
|
|
|
| 132 |
|
|
virtual void registerAttribute(const char *name, IAttribute *iface) {
|
| 133 |
|
|
AttributeType item(iface);
|
| 134 |
|
|
iface->allocAttrName(name);
|
| 135 |
|
|
listAttributes_.add_to_list(&item);
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
virtual IAttribute *getAttribute(const char *name) {
|
| 139 |
|
|
IAttribute *tmp;
|
| 140 |
|
|
for (unsigned i = 0; i < listAttributes_.size(); i++) {
|
| 141 |
|
|
tmp = static_cast<IAttribute *>(listAttributes_[i].to_iface());
|
| 142 |
|
|
if (strcmp(name, tmp->getAttrName()) == 0) {
|
| 143 |
|
|
return tmp;
|
| 144 |
|
|
}
|
| 145 |
|
|
}
|
| 146 |
|
|
return NULL;
|
| 147 |
|
|
}
|
| 148 |
|
|
|
| 149 |
|
|
virtual const char *getObjName() { return obj_name_.to_string(); }
|
| 150 |
|
|
|
| 151 |
|
|
virtual AttributeType getConfiguration() {
|
| 152 |
|
|
AttributeType ret(Attr_Dict);
|
| 153 |
|
|
ret["Name"] = AttributeType(getObjName());
|
| 154 |
|
|
ret["Attr"] = AttributeType(Attr_List);
|
| 155 |
|
|
|
| 156 |
|
|
IAttribute *tmp = NULL;
|
| 157 |
|
|
for (unsigned i = 0; i < listAttributes_.size(); i++) {
|
| 158 |
|
|
tmp = static_cast<IAttribute *>(listAttributes_[i].to_iface());
|
| 159 |
|
|
AttributeType item;
|
| 160 |
|
|
item.make_list(2);
|
| 161 |
|
|
item[0u].make_string(tmp->getAttrName());
|
| 162 |
|
|
item[1] = *static_cast<AttributeType *>(tmp);
|
| 163 |
|
|
ret["Attr"].add_to_list(&item);
|
| 164 |
|
|
}
|
| 165 |
|
|
return ret;
|
| 166 |
|
|
}
|
| 167 |
|
|
|
| 168 |
|
|
protected:
|
| 169 |
|
|
AttributeType listInterfaces_;
|
| 170 |
|
|
AttributeType listPorts_; // [['portname',iface],*]
|
| 171 |
|
|
AttributeType listAttributes_;
|
| 172 |
|
|
AttributeType logLevel_;
|
| 173 |
|
|
AttributeType obj_name_;
|
| 174 |
|
|
};
|
| 175 |
|
|
|
| 176 |
|
|
} // namespace debugger
|
| 177 |
|
|
|
| 178 |
|
|
#endif // __DEBUGGER_COMMON_ISERVICE_H__
|