modem: Adapt to the new network stack.

This is my first time using most of these API calls to the network stack, so,
reviews most welcome.
This commit is contained in:
Augustin Cavalier 2017-04-26 22:10:38 -04:00
parent 85a56a9d8e
commit bef731a331
6 changed files with 54 additions and 54 deletions

View File

@ -1,7 +1,7 @@
SubDir HAIKU_TOP src add-ons kernel network ppp ;
SubInclude HAIKU_TOP src add-ons kernel network ppp ipcp ;
# SubInclude HAIKU_TOP src add-ons kernel network ppp modem ;
SubInclude HAIKU_TOP src add-ons kernel network ppp modem ;
SubInclude HAIKU_TOP src add-ons kernel network ppp pap ;
SubInclude HAIKU_TOP src add-ons kernel network ppp pppoe ;
SubInclude HAIKU_TOP src add-ons kernel network ppp shared ;

View File

@ -8,7 +8,9 @@
#include <SupportDefs.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <net_buffer.h>
#include <net_stack.h>
class ModemDevice;
@ -30,12 +32,13 @@ class ModemDevice;
#define MODEM_INIT_KEY "Init"
#define MODEM_DIAL_KEY "Dial"
extern struct core_module_info *core;
extern net_stack_module_info *gStackModule;
extern net_buffer_module_info *gBufferModule;
#if DEBUG
// defined in ModemDevice.cpp
extern void dump_packet(struct mbuf *packet);
extern void dump_packet(net_buffer *packet);
#endif // DEBUG

View File

@ -9,7 +9,6 @@
#include "ACFCHandler.h"
#include "fcs.h"
#include <core_funcs.h>
#include <unistd.h>
#include <termios.h>
// for port settings
@ -21,7 +20,7 @@
#if DEBUG
static char sDigits[] = "0123456789ABCDEF";
void
dump_packet(struct mbuf *packet)
dump_packet(net_buffer *packet)
{
if(!packet)
return;
@ -404,49 +403,40 @@ ModemDevice::ConnectionLost()
status_t
ModemDevice::Send(struct mbuf *packet, uint16 protocolNumber)
ModemDevice::Send(net_buffer *packet, uint16 protocolNumber)
{
#if DEBUG
TRACE("ModemDevice: Send()\n");
dump_packet(packet);
#endif
if(!packet)
if (!packet)
return B_ERROR;
else if(InitCheck() != B_OK || protocolNumber != 0) {
m_freem(packet);
else if (InitCheck() != B_OK || protocolNumber != 0) {
gBufferModule->free(packet);
return B_ERROR;
} else if(!IsUp()) {
m_freem(packet);
} else if (!IsUp()) {
gBufferModule->free(packet);
return PPP_NO_CONNECTION;
}
// we might need room for our header
if(fACFC->LocalState() != ACFC_ACCEPTED) {
M_PREPEND(packet, 2);
if(!packet)
return B_ERROR;
}
int32 position = 0, length;
if(packet->m_flags & M_PKTHDR)
length = packet->m_pkthdr.len;
else
length = packet->m_len;
// we need a contiguous chunk of memory
packet = m_pullup(packet, length);
if(!packet)
return B_ERROR;
uint8 buffer[2 * (MODEM_MTU + PACKET_OVERHEAD)], *data = mtod(packet, uint8*);
uint8 buffer[2 * (MODEM_MTU + PACKET_OVERHEAD)];
// add header
if(fACFC->LocalState() != ACFC_ACCEPTED) {
if (fACFC->LocalState() != ACFC_ACCEPTED) {
NetBufferPrepend<uint8> bufferHeader(packet, 2);
uint8* data = bufferHeader.operator->();
data[0] = ALL_STATIONS;
data[1] = UI;
}
int32 position = 0, length = packet->size;
uint8* data;
if (gBufferModule->direct_access(packet, 0, length + 2, (void**)&data) != B_OK) {
ERROR("ModemDevice: Failed to access buffer!\n");
return B_ERROR;
}
// add FCS
uint16 fcs = 0xffff;
fcs = pppfcs16(fcs, data, length);
@ -468,11 +458,12 @@ ModemDevice::Send(struct mbuf *packet, uint16 protocolNumber)
buffer[position++] = FLAG_SEQUENCE;
// mark end of packet
m_freem(packet);
gBufferModule->free(packet);
data = NULL;
// send to modem
atomic_add((int32*) &fOutputBytes, position);
if(write(Handle(), buffer, position) < 0)
if (write(Handle(), buffer, position) < 0)
return PPP_NO_CONNECTION;
atomic_add((int32*) &fOutputBytes, -position);
@ -484,8 +475,7 @@ status_t
ModemDevice::DataReceived(uint8 *buffer, uint32 length)
{
// TODO: report corrupted packets to KPPPInterface
if(length < 3)
if (length < 3)
return B_ERROR;
// check FCS
@ -498,9 +488,12 @@ ModemDevice::DataReceived(uint8 *buffer, uint32 length)
if(buffer[0] == ALL_STATIONS && buffer[1] == UI)
buffer += 2;
mbuf *packet = m_gethdr(MT_DATA);
packet->m_len = packet->m_pkthdr.len = length - 2;
uint8 *data = mtod(packet, uint8*);
net_buffer* packet = gBufferModule->create(length - 2);
uint8* data;
if (gBufferModule->direct_access(packet, 0, length, (void**)&data) != B_OK) {
ERROR("ModemDevice: Failed to access buffer!\n");
return B_ERROR;
}
memcpy(data, buffer, length - 2);
return Receive(packet);
@ -508,14 +501,14 @@ ModemDevice::DataReceived(uint8 *buffer, uint32 length)
status_t
ModemDevice::Receive(struct mbuf *packet, uint16 protocolNumber)
ModemDevice::Receive(net_buffer *packet, uint16 protocolNumber)
{
// we do not need to lock because only the worker_thread calls this method
if(!packet)
if (!packet)
return B_ERROR;
else if(InitCheck() != B_OK || !IsUp()) {
m_freem(packet);
else if (InitCheck() != B_OK || !IsUp()) {
gBufferModule->free(packet);
return B_ERROR;
}

View File

@ -59,10 +59,10 @@ class ModemDevice : public KPPPDevice {
void FailedDialing();
void ConnectionLost();
virtual status_t Send(struct mbuf *packet, uint16 protocolNumber = 0);
virtual status_t Send(net_buffer *packet, uint16 protocolNumber = 0);
status_t DataReceived(uint8 *buffer, uint32 length);
// this will put the data into an mbuf and call Receive()
virtual status_t Receive(struct mbuf *packet, uint16 protocolNumber = 0);
virtual status_t Receive(net_buffer *packet, uint16 protocolNumber = 0);
private:
const char *fPortName, *fInitString, *fDialString;

View File

@ -53,7 +53,7 @@
/*
* Calculate a new fcs given the current fcs and the new data.
*/
u16 pppfcs16(u16 fcs, unsigned char *cp, int len)
u16 pppfcs16(u16 fcs, const unsigned char *cp, int len)
{
while (len--)
fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];

View File

@ -5,8 +5,6 @@
#include <KernelExport.h>
#include <driver_settings.h>
#include <core_funcs.h>
#include <net_module.h>
#include <KPPPInterface.h>
#include <KPPPModule.h>
@ -16,12 +14,12 @@
#define MODEM_MODULE_NAME NETWORK_MODULES_ROOT "ppp/modem"
struct core_module_info *core = NULL;
net_stack_module_info *gStackModule = NULL;
net_buffer_module_info *gBufferModule = NULL;
status_t std_ops(int32 op, ...);
static
bool
static bool
add_to(KPPPInterface& mainInterface, KPPPInterface *subInterface,
driver_parameter *settings, ppp_module_key_type type)
{
@ -60,14 +58,20 @@ _EXPORT
status_t
std_ops(int32 op, ...)
{
switch(op) {
switch (op) {
case B_MODULE_INIT:
if(get_module(NET_CORE_MODULE_NAME, (module_info**)&core) != B_OK)
if (get_module(NET_STACK_MODULE_NAME, (module_info**) &gStackModule) != B_OK)
return B_ERROR;
if (get_module(NET_BUFFER_MODULE_NAME,
(module_info **)&gBufferModule) != B_OK) {
put_module(NET_STACK_MODULE_NAME);
return B_ERROR;
}
return B_OK;
case B_MODULE_UNINIT:
put_module(NET_CORE_MODULE_NAME);
put_module(NET_BUFFER_MODULE_NAME);
put_module(NET_STACK_MODULE_NAME);
break;
default: